title: jdbcTemplate is null and throws null pointer exception
body: <p>I am new to Spring. I am trying to develop REST API using Spring web MVC. I am trying to fetch some data using JdbcTemplate from my database (MySQL). But when i run the code in the server I am getting <code>500- Internal Server Error</code> and the stack trace shows <code>java.lang.NullPointerException</code>. When i tried to debug the code i found out that jdbcTemplate variable has null value. I have tried every solution provided in web but still getting the same error. </p>
  
  <p>Here is my <strong>applicationContext.xml</strong> file</p>
  
  <pre><code>    &lt;context:component-scan base-package="com.cinema.repository" /&gt;
  
  
      &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
      &lt;bean id="userDao" class="com.cinema.repository.UserDaoImpl"&gt;
          &lt;property name="jdbcTemplate" ref="jdbcTemplate" /&gt;
      &lt;/bean&gt;
      &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
          &lt;property name="url" value="${jdbc.url}" /&gt;
          &lt;property name="username" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;context:property-placeholder location="/WEB-INF/jdbc.properties" /&gt;
  </code></pre>
  
  <p>My <strong>UserDaoImpl.java</strong> looks is: </p>
  
  <pre><code>public class UserDaoImpl implements UserDao {
  private JdbcTemplate jdbcTemplate;
  
  public JdbcTemplate getJdbcTemplate() {
      return jdbcTemplate;
  }
  @Autowired
  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
      this.jdbcTemplate = jdbcTemplate;
  }
  
  public User getUser(int userId) {
      String sql ="SELECT * FROM user_tbl WHERE user_id = ?";
      return this.jdbcTemplate.queryForObject(sql,new Object[]{new Integer(userId)}, new UserMapper() );
  }
  
  private static final class UserMapper implements RowMapper&lt;User&gt;{
      public User mapRow(ResultSet rs, int rowCount) throws SQLException {
          User u = new User();
          u.setUserId(rs.getInt("user_id"));
          u.setUserName(rs.getString("username"));
          u.setPassword(rs.getString("password"));
          u.setEmailId(rs.getString("email"));
          u.setRole(rs.getString("role"));
          return u;
      }
    }
  }
  </code></pre>
  
  <p>And my controller class is <strong>UserController.java</strong>:</p>
  
  <pre><code>@RestController
  @RequestMapping(value="/user")
  public class UserController {
      @RequestMapping(method = RequestMethod.GET, value="/{userId}", produces = {MediaType.APPLICATION_JSON_VALUE})
      ResponseEntity&lt;User&gt; getUser(@PathVariable int userId){
          UserDaoImpl user = new UserDaoImpl();
          User u = new User();
          u=user.getUser(userId);
          return new ResponseEntity&lt;User&gt;(u, HttpStatus.OK);
      }
  }
  </code></pre>
  
  <p>and i am getting the following stack trace while running the application:</p>
  
  <pre><code>10-Feb-2016 02:50:28.165 SEVERE [tomcat-http--5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [spring] in context with path [/booking] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
  java.lang.NullPointerException
  at com.cinema.repository.UserDaoImpl.getUser(UserDaoImpl.java:31)
  at com.cinema.web.UserController.getUser(UserController.java:21)
  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:222)
  at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
  at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
  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:142)
  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:518)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:277)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>Please tell me what i am doing wrong here. I have been stuck on this problem for a week now and still cannot make my code work. Thanks in advance. </p>