1

I'm getting the NullPointerException and I can't figure out where are my mistakes.

The error is in selectByExample function. The code is:

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

The applicationContext is the follow:

       <!-- 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>

Here there is the error:

[11/07/14 11.19.33:848 CEST] 0000004f servlet E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Generata eccezione non rilevata in uno dei metodi di servizio del servlet dispatcher nell'applicazione TestEAR. Creata eccezione: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) at javax.servlet.http.HttpServlet.service(HttpServlet.java:738) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) at javax.servlet.http.HttpServlet.service(HttpServlet.java:831) at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502) at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181) at 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(HttpICLReadCallback.java:83) at com.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: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) Caused by: 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at 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.handle(AnnotationMethodHandlerAdapter.java:434) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) ... 25 more

[11/07/14 11.19.33:856 CEST] 0000004f FfdcProvider W com.ibm.ws.ffdc.impl.FfdcProvider logIncident FFDC1003I: Emesso evento FFDC in 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) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at 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.handle(AnnotationMethodHandlerAdapter.java:434) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) at javax.servlet.http.HttpServlet.service(HttpServlet.java:738) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) at javax.servlet.http.HttpServlet.service(HttpServlet.java:831) at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1657) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502) at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181) at 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(HttpICLReadCallback.java:83) at com.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: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)

8
  • 1
    Where does the exception occure? Commented Jul 11, 2014 at 9:15
  • I added the information to the question. Commented Jul 11, 2014 at 9:24
  • What's at these lines: TestService.java:81 and controller.java:46? Commented Jul 11, 2014 at 9:37
  • Try putting a breakpoint in this code, which I assume is yours:java.lang.NullPointerException at com.service.test.TestService.test(TestService.java:81) at com.test.controller.ReturnPage(controller.java:46) at Commented Jul 11, 2014 at 9:37
  • 1
    We need the complete code - it is impossible to know what TestMapper returns. Also the line private TestMapper TestMapper is bad - if it compiles (does it?), then there would be no way to separate a call to a static method from a call to an instance method. private TestMapper testMapper is the usual syntax. Commented Jul 11, 2014 at 9:50

1 Answer 1

2

As you use Spring you should avoid explicitly creating your beans (using new operator) - the framework should do it for you. And the bean instance should be already in Spring context. If you instantiate your service bean with new nothing will be autowired. Thats why you will have null instead of your mapper instance.

Please look info the Spring documentation about dependency injection and beans lifecycle in Spring applications http://docs.spring.io/spring/docs/3.0.x/reference/beans.html#beans-factory-collaborators

Update:

Autowiring TestService into your controller may solve your issue.

@Controller
public class Controller {
    @Autowired private TestService testService;
    ...
    @RequestMapping
    public String returnPage() {
        ...
        testService.sediMigrate(); 
        ...
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What do you mean with "avoid explicity creating your beans"?
selectByExample is generated by MyBatis Generator and it is not static.
If I understood well, I believe I didn't create the beans for TestService, but I used this line <context:component-scan base-package="com.service.test"/>
TestService ms = new TestService(); means you did. I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.