带有MyBatis的java Mapper上的NullPointerException

时间:2014-07-11 09:13:01

标签: java spring mybatis mybatis-generator

我得到了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>

这里有错误:

  

[11/07/14 11.19.33:848 CEST] 0000004f servlet E.   com.ibm.ws.webcontainer.servlet.ServletWrapper服务SRVE0068E:   Generata eccezione non rilevata in uno dei metodi di servizio del   servlet调度员nell&applicaione TestEAR。 Creata eccezione:   org.springframework.web.util.NestedServletException:Request   处理失败;嵌套异常是java.lang.NullPointerException     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)     在   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:738)at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:831)at   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(WebApp.java:3935)     在   com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)     在   com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)     在   com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)     在   com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)     在   com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)     在   com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)     在   com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)     在   com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.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)     在com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)at at   com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)at at   com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)     在com.ibm.io.async.ResultHandler $ 2.run(ResultHandler.java:905)at   com.ibm.ws.util.ThreadPool $ Worker.run(ThreadPool.java:1613)引起:   java.lang.NullPointerException at   com.service.test.TestService.test(TestService.java:81)at   com.test.controller.ReturnPage(controller.java:46)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   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)     在   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)     ......还有25个

     

[11/07/14 11.19.33:856 CEST] 0000004f FfdcProvider W   com.ibm.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I:Emesso evento   FF:C:\ Program   FILES \ IBM \的WebSphere \ AppServer的\型材\ AppSrv01 \日志\ 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)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   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)     在   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)at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:831)at   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(WebApp.java:3935)     在   com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)     在   com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)     在   com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)     在   com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)     在   com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)     在   com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)     在   com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)     在   com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.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)     在com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)at at   com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)at at   com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)     在com.ibm.io.async.ResultHandler $ 2.run(ResultHandler.java:905)at   com.ibm.ws.util.ThreadPool $ Worker.run(ThreadPool.java:1613)

1 个答案:

答案 0 :(得分:2)

当你使用Spring时,你应该避免显式创建你的bean(使用new运算符) - 框架应该为你做。并且bean实例应该已经在Spring上下文中。如果使用new实例化服务bean,则不会自动装配任何内容。这就是为什么你将null而不是你的映射器实例。

请查看有关Spring应用程序http://docs.spring.io/spring/docs/3.0.x/reference/beans.html#beans-factory-collaborators

中依赖注入和bean生命周期的Spring文档

<强>更新

TestService自动装入您的控制器可以解决您的问题。

@Controller
public class Controller {
    @Autowired private TestService testService;
    ...
    @RequestMapping
    public String returnPage() {
        ...
        testService.sediMigrate(); 
        ...
    }
}