英文:
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: -> 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
"com.board.dao.BoardDao.listboard(com.board.dto.BoardDto)" because "this.bDao" 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("/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;
}
}
controller is fine.. i think..
service
@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 ;
}
}
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<BoardDto> listboard(BoardDto dto) {
System.out.println("dao.");
List<BoardDto> result = session.selectList("boarddate.listboard", dto);
return result;
}
}
<?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">
<!-- Root Context: defines shared resources visible to all other web components -->
<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: -> 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>
<!-- DI -->
<bean class="com.board.service.BoardService"></bean>
<bean class="com.board.dao.BoardDao">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
</beans>
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<BoardDto> listboard(BoardDto dto) {
System.out.println("dao.");
List<BoardDto> result = session.selectList("boarddate.listboard", 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论