无法调用“”因为“”为空。

huangapple go评论79阅读模式
英文:

Cannot invoke "" because "" is null

问题

我终于找到问题标签了。
为什么会出现这个错误 'boom'????
我不明白,花了两天时间……

“com.board.dao.BoardDao.listboard(com.board.dto.BoardDto)”,因为“this.bDao”为空**
	at com.board.service.BoardService.listboard(BoardService.java:19)
	at com.board.controller.BoardController.boardlist(BoardController.java:25)
    at com.board.controller.BoardController.boardlist(BoardController.java:26)
	…

控制器:

@Controller
public class BoardController {
	
	@Autowired
	BoardService service;
	
	@RequestMapping("/board")
	public ModelAndView boardlist(ModelAndView mv, BoardDto dto) {
		System.out.println("boardlist ");
		
		System.out.println(dto + "dto");
		List<BoardDto> dtolist = service.listboard(dto);
		mv.addObject("boardlist", dtolist);
		mv.setViewName("boardlist");
		return mv;
	}
}

控制器没问题,我想……

服务:

@Service
public class BoardService {
	
	@Autowired
	BoardDao bDao;

	public List<BoardDto> listboard(BoardDto dto) {
		System.out.println("boardservice");
		List<BoardDto> result = bDao.listboard(dto);
		return result ;
	}

}

为什么会在这个位置出现错误???T.T

我认为这个方法有错误,但我不知道为什么会出错。

数据访问对象:

public class BoardDao extends SqlSessionDaoSupport{
	
	@Autowired
	SqlSessionTemplate session;

	public List<BoardDto> listboard(BoardDto dto) {
		System.out.println("dao.");
		List<BoardDto> result = session.selectList("boarddate.listboard", dto);
		return result;
	}

}

XML 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<!-- 根上下文:定义对所有其他 Web 组件可见的共享资源 -->

	<bean id="dataSource" 
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
		<property name="username" value="board"/>
		<property name="password" value="1234"/>
	</bean>	

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- classpath: -&gt; src/main/resources -->
		<property name="configLocation" value="classpath:myBatis/myBatis-config.xml"/>	
	</bean>

	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
	</bean>
	
	<!-- 依赖注入 -->
	<bean class="com.board.service.BoardService"></bean>
	<bean class="com.board.dao.BoardDao">
		<property name="sqlSessionTemplate" ref="sqlSession"/>
	</bean>
	
</beans>

为什么会出现 'boom' 错误?我不明白…… T.T

---> 添加更多错误信息。

英文:

i finally find it question tab.
why this error is 'boom'?????
i didn't understand spend two days.....

    **java.lang.NullPointerException: Cannot invoke 
    &quot;com.board.dao.BoardDao.listboard(com.board.dto.BoardDto)&quot; because &quot;this.bDao&quot; is null**
	at com.board.service.BoardService.listboard(BoardService.java:19)
	at com.board.controller.BoardController.boardlist(BoardController.java:25)
    at com.board.controller.BoardController.boardlist(BoardController.java:26)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1626)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Unknown Source)

controller

@Controller
public class BoardController {
	
	@Autowired
	BoardService service;
	
	
	@RequestMapping(&quot;/board&quot;)
	public ModelAndView boardlist (ModelAndView mv ,BoardDto dto) {
		System.out.println(&quot;boardlist &quot;);
		
		System.out.println(dto+&quot;dto&quot;);
		List&lt;BoardDto&gt; dtolist = service.listboard(dto);
		mv.addObject(&quot;boardlist&quot;, dtolist);
		mv.setViewName(&quot;boardlist&quot;);
		return mv;
	}
}

controller is fine.. i think..

service

@Service
public class BoardService {
	
	@Autowired
	BoardDao bDao;

	public List&lt;BoardDto&gt; listboard(BoardDto dto) {
		System.out.println(&quot;boardservice&quot;);
		List&lt;BoardDto&gt; result = bDao.listboard(dto);
		return result ;
	}

}

this position is error why?? T.T

i think this method is error position but i don't know understand. why???

dao

public class BoardDao extends SqlSessionDaoSupport{
	
	@Autowired
	SqlSessionTemplate session;

	public List&lt;BoardDto&gt; listboard(BoardDto dto) {
		System.out.println(&quot;dao.&quot;);
		List&lt;BoardDto&gt; result = session.selectList(&quot;boarddate.listboard&quot;, dto);
		return result;
	}

}
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd&quot;&gt;
	
	&lt;!-- Root Context: defines shared resources visible to all other web components --&gt;


	&lt;bean id=&quot;dataSource&quot; 
		class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;
		&lt;property name=&quot;driverClassName&quot; value=&quot;oracle.jdbc.driver.OracleDriver&quot;/&gt;
		&lt;property name=&quot;url&quot; value=&quot;jdbc:oracle:thin:@localhost:1521:orcl&quot;/&gt;
		&lt;property name=&quot;username&quot; value=&quot;board&quot;/&gt;
		&lt;property name=&quot;password&quot; value=&quot;1234&quot;/&gt;
	&lt;/bean&gt;	


	&lt;bean id=&quot;sqlSessionFactory&quot; class=&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;&gt;
		&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
		&lt;!-- classpath: -&gt; src/main/resources --&gt;
		&lt;property name=&quot;configLocation&quot; value=&quot;classpath:myBatis/myBatis-config.xml&quot;/&gt;	
	&lt;/bean&gt;



	&lt;bean id=&quot;sqlSession&quot; class=&quot;org.mybatis.spring.SqlSessionTemplate&quot;&gt;
		&lt;constructor-arg name=&quot;sqlSessionFactory&quot; ref=&quot;sqlSessionFactory&quot;/&gt;
	&lt;/bean&gt;
	&lt;!-- DI  --&gt;
	&lt;bean class=&quot;com.board.service.BoardService&quot;&gt;&lt;/bean&gt;
	&lt;bean class=&quot;com.board.dao.BoardDao&quot;&gt;
		&lt;property name=&quot;sqlSessionTemplate&quot; ref=&quot;sqlSession&quot;/&gt;
	&lt;/bean&gt;
	
&lt;/beans&gt;

root-context.xml

why error is 'boom'? i didn't understand.... T.T

--> edit more infomation to error

答案1

得分: 7

我认为你的问题与你的 BoardDao 类缺少 @Component 注解有关。组件应该有一个 @Component 注解,以实例化单例对象,然后注入到你的服务层中。

@Component
public class BoardDao extends SqlSessionDaoSupport {
    
    @Autowired
    SqlSessionTemplate session;

    public List<BoardDto> listboard(BoardDto dto) {
        System.out.println("dao.");
        List<BoardDto> result = session.selectList("boarddate.listboard", dto);
        return result;
    }

}

如果问题仍然存在,你可以尝试使用 @Repository 注解。不幸的是,我没有使用过 SqlSessionDaoSupport 类,所以我不确定最适合它的最佳注解是什么。

英文:

I think your problem is related with the lack of a @Component annotation in your BoardDao class.
The component should have a @Component annotation to instantiate the singleton to be injected in your Service Layer.

@Component
public class BoardDao extends SqlSessionDaoSupport{

    @Autowired
    SqlSessionTemplate session;

    public List&lt;BoardDto&gt; listboard(BoardDto dto) {
        System.out.println(&quot;dao.&quot;);
        List&lt;BoardDto&gt; result = session.selectList(&quot;boarddate.listboard&quot;, dto);
        return result;
    }

}

If the problem persist, you may try with the @Repository annotation. Sadly I haven't used the class SqlSessionDaoSupport, so I don't know exactly the best annotation for that.

答案2

得分: 0

在我的情况下是使用了一个 Map,我想增加它的值,但最初这个值可能不存在,因此它是 null。

修复方法是添加一个验证,像这样:

map.put(0, map.get(0) != null ? map.get(0) + 1 : 1);
英文:

In my case it was with a Map, I wanted to increase its value, but this initial one will not exist, therefore it is null.

map.put(0, map.get(0) 1);

It was fixed by adding a validation, like so:

map.put(0, map.get(0) != null ? map.get(0) + 1 : 1);

huangapple
  • 本文由 发表于 2020年9月22日 08:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64001667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定