title: Servlet.service() for servlet [dispatcher] in context with path [/***] threw exception
body: <p>I am getting below error while creating spring maven rest webservice. When trying to call DAO class with bean name defined in application_config.xml, getting NullPointerException</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/riceAppService] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
  java.lang.NullPointerException
      at com.ganeshTrading.riceAppService.controller.UserController.getAllUsers(UserController.java:46)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>Here, is my code</p>
  
  <p><strong>UserController.java</strong></p>
  
  <pre><code>    @RestController
  public class UserController {
  
      private JdbcUserDAO jdbcUserDAO;
  
  
      public JdbcUserDAO getJdbcUserDAO() {
          return jdbcUserDAO;
      }
  
      public void setJdbcUserDAO(JdbcUserDAO jdbcUserDAO) {
          this.jdbcUserDAO = jdbcUserDAO;
      }
  
      @RequestMapping("/")
      @ResponseBody
      public String welcome() {
          return "Welcome to RestTemplate Example.";
      }
  
      @RequestMapping(value="/users", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody   
      public List&lt;User&gt; getAllUsers() {
          System.out.println("jdbcUserDAO"+jdbcUserDAO);
          List&lt;User&gt; userList = jdbcUserDAO.getAllUsers();
          System.out.println(userList);
          return userList;
      }
  
  }
  </code></pre>
  
  <p><strong>UserDAO.java</strong></p>
  
  <pre><code>public interface UserDAO {
  
      public List&lt;User&gt; getAllUsers();
  }
  </code></pre>
  
  <p><strong>JdbcUserDAO.java</strong></p>
  
  <pre><code>public class JdbcUserDAO implements UserDAO {
  
      DataSource dataSource;
  
  
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  
  
      @Override
      public List&lt;User&gt; getAllUsers() {
          String sql = "select * from customer";
          JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
          System.out.println("getJdbcTemplate() "+jdbcTemplate);
          List&lt;User&gt; userList = jdbcTemplate.query(sql, new UserMapper());
          return userList;
      }
  
  }
  </code></pre>
  
  <p><strong>servlet-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:schemaLocation=" http://www.springframework.org/schema/beans  
   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-4.3.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"&gt;
  
  
  
  &lt;context:annotation-config /&gt;
  &lt;mvc:annotation-driven /&gt;
  
  &lt;import resource="classpath:application_config.xml" /&gt; 
  &lt;context:component-scan base-package="com.ganeshTrading.riceAppService" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>application_config.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"&gt;
  
  
      &lt;bean id="jdbcUserDAO" class="com.ganeshTrading.riceAppService.dao.impl.JdbcUserDAO"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
     &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/ganeshricetrading"&gt;&lt;/property&gt;
      &lt;property name="username" value="root"&gt;&lt;/property&gt;
      &lt;property name="password" value="sql"&gt;&lt;/property&gt;
     &lt;/bean&gt;    
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;web-app xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns="xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"&gt;
        &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>ApplicationConfiguration.java</strong></p>
  
  <pre><code>@ComponentScan(basePackages = {"com.ganeshTrading.riceAppService"})
  @Configuration
  @EnableWebMvc
  public class ApplicationConfiguration {
  
  }
  </code></pre>
  
  <p>Please advise what is wrong in my code. Thanks in advance.</p>