title: NPE SessionFactory in Spring/Hibernate integration
body: <p>I'm a newbie in Spring and Hibernate and I'm having a NPE in this line:</p>
  
  <pre><code>Session session = getSessionFactory().getCurrentSession();
  </code></pre>
  
  <hr>
  
  <p>This is my UserDAO class</p>
  
  <pre><code>public class UserDAO implements IUserDAO{
  
           private SessionFactory sessionFactory;
  
            public SessionFactory getSessionFactory() {
                return sessionFactory;
            }
            public void setSessionFactory(SessionFactory sessionFactory) {
                this.sessionFactory = sessionFactory;
            }
  
          @Override
          public Integer updateUser(User user) {
              // TODO Auto-generated method stub
              return null;
          }
  
          @Override
          public User searchUserIfExists(String username, String password) {
              Session session = getSessionFactory().getCurrentSession();
  
              Query query = session.createQuery(SEARCH_USER_ACCT);
              query.setParameter("username", username);
              query.setParameter("password",password);
              List&lt;User&gt; userList = query.list();
  
              System.out.println(userList.size());
              User user = null;
              return user;
          }
  </code></pre>
  
  <p>And this is my UserController</p>
  
  <pre><code>@SessionAttributes(value = {"userSession"})
  @Controller
  public class UserController {
  
      private IUserDAO userDAO;  
  
      @Autowired  
      public UserController(IUserDAO userDAO) {  
          this.userDAO = userDAO;  
      }  
  
      @RequestMapping(value = "/processLogin.htm", method=RequestMethod.POST)
      public String login(@ModelAttribute User user,@RequestParam("username") String username,
                @RequestParam("password") String password,
                HttpServletRequest request, HttpServletResponse response){
  
          userDAO = new UserDAO();
          User userOb = userDAO.searchUserIfExists(username, password);
  
          if(userOb != null){
              ModelAndView modelAndView = new ModelAndView();
              String role = userOb.getUserRole();
              switch(role){
                  case "admin":   
                      modelAndView.addObject("userSession", userOb);
                      modelAndView.addObject("userId", userOb.getUserId());
                      modelAndView.setViewName("/adminPage_admin");
                      break;
                  case "customer":
                      modelAndView.addObject("userSession", userOb);
                      modelAndView.addObject("userId", userOb.getUserId());
                      modelAndView.setViewName("/customerpage_customer");
                      break;
  
          }
  
  
  
      }
          return "";
      }
  }
  </code></pre>
  
  <p>This is my xml configuration for spring</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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
    &lt;!-- Enable @Controller annotation support --&gt;
   &lt;mvc:annotation-driven /&gt;
   &lt;mvc:default-servlet-handler/&gt;
    &lt;!-- Map simple view name such as "test" into /WEB-INF/test.jsp --&gt;
    &lt;beans:bean   class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;      
          &lt;beans:property name="prefix" value="/jsp" /&gt;       
          &lt;beans:property name="suffix" value=".jsp" /&gt;   
    &lt;/beans:bean&gt;
  
    &lt;!-- Scan classpath for annotations (eg: @Service, @Repository etc) --&gt;
  
      &lt;context:component-scan base-package="com.nacv2.*"/&gt;
  
  
       &lt;context:spring-configured/&gt;
  
    &lt;!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with 
         username root and blank password. Change below if it's not the case --&gt;
    &lt;bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/db_nac"/&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value=""/&gt;
      &lt;property name="validationQuery" value="SELECT 1"/&gt;
    &lt;/bean&gt;
  
    &lt;!-- Hibernate Session Factory --&gt;
    &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="myDataSource"/&gt;
      &lt;property name="packagesToScan"&gt;
  
        &lt;array&gt;
          &lt;value&gt;com.nacv2.model.*&lt;/value&gt;
        &lt;/array&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
              &lt;prop key="hibernate.connection.pool_size"&gt;${hibernate.connection.pool_size}&lt;/prop&gt;
        &lt;/props&gt;
  
      &lt;/property&gt;
    &lt;/bean&gt;
  
      &lt;bean id="userDAO" class="com.nacv2.dataaccess.UserDAO"&gt;  
              &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;  
      &lt;/bean&gt;
  
    &lt;!-- Hibernate Transaction Manager --&gt;
     &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="mySessionFactory"/&gt;
    &lt;/bean&gt;   
  
    &lt;!-- Activates annotation based transaction management --&gt;
  
     &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; 
  
  
  &lt;/beans&gt;    
  </code></pre>