title: Spring + mybatis = java.lang.NullPointerException
body: <p>I am very new to Spring and mybatis so I am sure that I am doing more than one thing wrong here. I am getting NPE when I call getWorkbasketResponse(). Can you pleas help me out. </p>
  
  <pre><code>@Controller
  public class MySubscribeController {
  
      private static final Logger logger = LoggerFactory.getLogger(MySubscribeController.class);
  
      @RequestMapping(value = "/workbasket/subscribe", method = RequestMethod.POST)
      public @ResponseBody String subWorkBasket(@RequestBody WorkbasketRequest subWorkbasket)  {  
          logger.info("Workbasket");
          //new FuseboxPush(subWorkbasket);
          MyDAO dao = new MyDAO();
          dao.getWorkbasketResponse();
          return "{result:\"true\"}";     
      }
  }
  </code></pre>
  
  <p>Here is my DAO</p>
  
  <pre><code>public class MyDAO {
      private static final Logger logger = LoggerFactory.getLogger(MyDAO.class);
      private SqlSessionFactory sqlSessionFactory;
      public MyDAO() { }
  
      public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
          this.sqlSessionFactory = sqlSessionFactory;
      }
      public WorkbasketResponse getWorkbasketResponse() {
  
          WorkbasketResponse response = null;
          try {
              SqlSession session = sqlSessionFactory.openSession();
  
              try {
  
                  response = (WorkbasketResponse)session.selectOne("com.fmr.myapp.domain.response.WorkbasketResponse.getWorkbasketResponse");
              }
              catch(Exception e) {
                  logger.error(e.getMessage());
              }
              finally {
                  session.close();
              }
          }
          catch(Exception e) {
              e.printStackTrace();
                          //Thorws java.lang.NullPointerException !!
          }
          return response;
      }
  }
  </code></pre>
  
  <p>Here is what my root-context.xml looks like</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="url" value="jdbc:derby://someserver:1527/myapp;create=true;user=derbyuser;password=derbyuser"/&gt;
      &lt;property name='driverClassName' value='org.apache.derby.jdbc.ClientDriver'/&gt;
  &lt;/bean&gt;
  &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="configLocation" value="classpath:mybatis-config.xml"/&gt;
  &lt;/bean&gt;
  &lt;bean id="myDAO" class="com.fmr.myapp.service.MyDAO"&gt;
      &lt;property name="sqlSessionFactory" ref="sqlSessionFactory"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>mybatis-config.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE configuration
      PUBLIC '-//mybatis.org//DTD Config 3.0//EN'
      'http://mybatis.org/dtd/mybatis-3-config.dtd'&gt;
  &lt;configuration&gt;
      &lt;environments default='development'&gt;
          &lt;environment id='development'&gt;
              &lt;transactionManager type='JDBC'/&gt;
              &lt;dataSource type='POOLED'&gt;    
              &lt;property name='driver' value='org.apache.derby.jdbc.ClientDriver'/&gt;
              &lt;property name='url' value='jdbc:derby://someserver:1527/myapp;create=true;user=derbyuser;password=derbyuser'/&gt;
              &lt;/dataSource&gt;
          &lt;/environment&gt;
      &lt;/environments&gt;
      &lt;mappers&gt;
          &lt;mapper resource="myappMapper.xml"/&gt;
      &lt;/mappers&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Exception that I am getting </p>
  
  <pre><code>java.lang.NullPointerException
      at com.fmr.myapp.service.MyDAO.getWorkbasketResponse(MyDAO.java:26)
      at com.fmr.myapp.controllers.MySubscribeController.subWorkBasket(MySubscribeController.java:35)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      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.doPost(FrameworkServlet.java:789)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1686)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>