Java Mapper和MyBatis上的NullPointerException
我得到NullPointerException
,我无法弄清楚我的错误在哪里。
该错误在selectByExample
函数中。 代码是:
package com.service.test;
@Service
public class TestService {
[...]
@Autowired
private TestMapper TestMapper;
[...]
public List<Short> test(){
List<Short> lista = new ArrayList<Short>();
testExample me = new testExample();
me.or().andTestEqualTo(1);
List<test> listTest = TestMapper.selectByExample(me); //line 81
for(int i=0; i<listTest.size(); i++){
lista.add(listTest.get(i).getNuovoDb());
}
return lista;
}
Controller.java
TestService ms = new TestService();
List<Short> listTest = ms.sediMigrate(); //line 46
applicationContext如下:
<!-- enable autowire -->
<context:annotation-config />
<context:component-scan base-package="com.service.test"/>
[...]
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.test.**"/>
<property name="sqlSessionFactoryBeanName" value="exSqlSessionFactory"/>
</bean>
这里有错误:
[14年11月7日33年11月19日:848 CEST] 0000004f的servletÊcom.ibm.ws.webcontainer.servlet.ServletWrapper服务SRVE0068E:Generata eccezione非rilevata在UNO DEI metodi二SERVIZIO德尔servlet调度nell'applicazione TestEAR。 Creata eccezione:org.springframework.web.util.NestedServletException:请求处理失败; 嵌套异常是在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)上的org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)上javax.servlet中的java.lang.NullPointerException .http.HttpServlet.service(HttpServlet.java:738)在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)在javax.servlet.http.HttpServlet.service(HttpServlet.java:831)在com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657)位于com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)com.ibm.ws.webcontainer。 servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)在com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)在com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(Web应用程序。 java:3935)at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)at com.ibm.ws.webcontainer.WebContainer.handle 请求(WebContainer.java:931)at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)at com .ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)在com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)在玉米.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)在com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)在玉米.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)at com.ibm.io.async.AsyncChannelFuture .fireCompletionActions(AsyncChannelFuture.java:161)at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:2 04)at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)at com.ibm.io.async.ResultHandler $ 2.run(ResultHandler.java:905)at com.ibm.ws.util。 ThreadPool $ Worker.run(ThreadPool.java:1613)引起:java.lang.NullPointerException在com.service.test.TestService.test(TestService.java:81)在com.test.controller.ReturnPage(controller.java: 46)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在java.lang.reflect中.Method.invoke(Method.java:600)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod (AnnotationMethodHandlerAdapter.java:446)at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.han DLE(AnnotationMethodHandlerAdapter.java:434)在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)在org.springframework .web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)... 25更多
[11/07/14 11.19.33:856 CEST] 0000004f FfdcProvider W com.ibm.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I:Emesso evento FFDC位于C: Program Files IBM WebSphere AppServer profiles AppSrv01 logs ffdc server1_5e0b5e0b_14.07.11_11.19.33.8537970037945028520402.txt com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest()309 [11/07/14 11.19.33:857 CEST] 0000004f webapp E com.ibm。 ws.webcontainer.webapp.WebApp logServletError SRVE0293E:[Errore servlet] - [dispatcher]:java.lang.NullPointerException at com.service.test.TestService.test(TestService.java:81)at com.test.controller.ReturnPage( controller.java:46)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)在java的.lang.reflect.Method.invoke(Method.java:600)at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(Handler MethodInvoker.java:175)在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java: 434)在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)在org.springframework.web.servlet.FrameworkServlet .processRequest(FrameworkServlet.java:961)在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)在javax.servlet.http.HttpServlet.service(HttpServlet.java:738)在org.springframework。 web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)位于javax.servlet.http.HttpServlet.service(HttpServlet.java:831)位于com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java: 1657)在com.ibm.ws.webcontainer.servlet.ServletW rapper.handleRequest(ServletWrapper.java:939)在com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)在com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java: 181)com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3935)at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)at com.ibm.ws .webcontainer.WebContainer.handleRequest(WebContainer.java:931)at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java :186)at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java :511)at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCa llback.java:83)在com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)在com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)在COM .ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)at com.ibm.io.async.ResultHandler.complete(ResultHandler .java:204)at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)at com.ibm.io.async.ResultHandler $ 2.run(ResultHandler.java:905)at com.ibm.ws .util.ThreadPool $ Worker.run(ThreadPool.java:1613)
当你使用Spring时,你应该避免显式创建你的bean(使用new
运算符) - 框架应该为你做。 而且这个bean实例应该已经在Spring上下文中了。 如果你用new
实例化你的服务bean,那么将自动装配。 这就是为什么你会有null
而不是你的映射器实例。
请查看Spring应用程序中关于依赖注入和bean生命周期的Spring文档http://docs.spring.io/spring/docs/3.0.x/reference/beans.html#beans-factory-collaborators
更新:
将TestService
自动装入控制器可能会解决您的问题。
@Controller
public class Controller {
@Autowired private TestService testService;
...
@RequestMapping
public String returnPage() {
...
testService.sediMigrate();
...
}
}
链接地址: http://www.djcxy.com/p/27885.html