title: HibernateTemplate showing java.lang.NullPointerException on using find() function
body: <p>I am very new to Spring+Hibernate and am working on a very basic web project. Simple form is used and has two fields username and password. It has a submit button. When clicking on submit I am supposed to check if username and password exists in the database or not.This is my AuthenticationService Class</p>
  
  <pre><code>package com.service;
  import java.util.List;
  import org.hibernate.SessionFactory;
  import org.springframework.orm.hibernate3.HibernateTemplate;
  import org.springframework.stereotype.Service;
  
  @Service
  public class AuthenticateService{
  
      private HibernateTemplate hibernateTemplate;
  
      public void setSessionFactory(SessionFactory sessionFactory)
      {
          this.hibernateTemplate=new HibernateTemplate(sessionFactory);
      }
  
      public boolean verifyUsernameAndPassword(String username,String password)
      {
          System.out.println("Into the Service Class");
          boolean userStatus=false;
          try
          {
  
              List userObj = hibernateTemplate.find("from user u where u.username=? and u.password=?",username,password);     
              if(userObj.size()!=0)
              {
                  userStatus=true;
              }
  
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
  
          return userStatus;
      }
  
  }
  </code></pre>
  
  <p>This is my bean configuration file:</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.xsd"&gt;
  
      &lt;bean id="dataSourceBean" 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://127.0.0.1:3306/springmvc"&gt;&lt;/property&gt;
          &lt;property name="username" value="root"&gt;&lt;/property&gt;
          &lt;property name="password" value=""&gt;&lt;/property&gt;        
      &lt;/bean&gt; 
      &lt;!-- org.springframework.orm.hibernate3.LocalSessionFactoryBean --&gt;
       &lt;bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSourceBean"&gt;&lt;/property&gt;
          &lt;property name="mappingResources"&gt;
              &lt;value&gt;com/pojo/user.hbm.xml&lt;/value&gt;                    
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
                &lt;props&gt;
                    &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
                    &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                &lt;/props&gt;
           &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="hibernateTemplateBean" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;
          &lt;property name="sessionFactory" ref="sessionFactoryBean"&gt;&lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="authenticateServiceBean" class="com.service.AuthenticateService"&gt;
          &lt;property name="hibernateTemplate" ref="hibernateTemplateBean"&gt;&lt;/property&gt;
      &lt;/bean&gt; 
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>This is my Login Controller:</p>
  
  <pre><code>package com.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.servlet.ModelAndView;
  
  import com.service.AuthenticateService;
  
  @Controller
  @RequestMapping("/login.spring")
  
  public class LoginController {
  
      @Autowired
      AuthenticateService authenticateService;
      @RequestMapping(method= RequestMethod.POST)
      public ModelAndView processCredentials(@RequestParam("username")String username ,@RequestParam("password")String password)
      {
          System.out.println("Inside username"+username + "  password"+password);
  
          String message="Invalid Credentials";
  
          if(authenticateService.verifyUsernameAndPassword(username, password)==true)
          {
              message="Welcome"+username;
          }
  
  return new ModelAndView("results","message",message);
      }
  }
  </code></pre>
  
  <p>When I run the application I get the following stacktrace :</p>
  
  <pre><code>java.lang.NullPointerException
      at com.service.AuthenticateService.verifyUsernameAndPassword(AuthenticateService.java:31)
      at com.controller.LoginController.processCredentials(LoginController.java:26)
      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.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
      at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>I am sure the application goes to the Service class as I have printed the "Inside the Service Class". I don't know why its giving me a NullPointer.</p>