title: Spring 3.2 and Hibernate 4 - Getting NullPointerException due to SessionFactory being null
body: <p>I'm trying to convert my very small app to Spring 3.2 and Hibernate 4, but I can't get it to run properly with sessions.  Some info below this paragraph, but ultimately the sessionFactory is always null everytime it gets to the DAO and I can't figure out why.</p>
  
  <p><strong>Comparing the console to Hibernate 3, Hibernate 3 shows this upon startup:</strong><br>Nov 23, 2012 10:51:52 PM org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory
  INFO: Building new Hibernate SessionFactory</p>
  
  <p><strong>Stack trace:</strong><br>
  java.lang.NullPointerException
  dao.CSSDAOImpl.selectAllCSS(CSSDAOImpl.java:21)</p>
  
  <p><strong>hibernate-cfg.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" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt;
  
      &lt;bean id="myDataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"&gt;&lt;/property&gt;
          &lt;property name="url" value="jdbc:derby:C:\Users\Steven\MyDB"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="packagesToScan" value="net.form" /&gt;
          &lt;property name="dataSource" ref="myDataSource"&gt;&lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.DerbyTenSevenDialect
                  &lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;net.form.StyleChooser&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="CSSDAO" class="dao.CSSDAOImpl"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>DAO:</strong></p>
  
  <pre><code>package dao;
  
  import java.util.List;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.transaction.annotation.Transactional;
  
  import net.form.StyleChooser;
  
  public class CSSDAOImpl implements CSSDAO {
  
      private SessionFactory sessionFactory;
  
      @Autowired
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      @Transactional
      public List selectAllCSS() {
          Session session = sessionFactory.openSession();
          Transaction tx = session.beginTransaction();
          List l = session.createCriteria(StyleChooser.class).list();
          session.flush();
          tx.commit();
          return l;
      }
  
      @Transactional
      public StyleChooser selectCSSById(Integer ID) {
          Session session = sessionFactory.openSession();
          Transaction tx = session.beginTransaction();
          StyleChooser sc = (StyleChooser) session.get(StyleChooser.class, ID);
          session.flush();
          tx.commit();        
          return sc;
      }
  
      @Transactional
      public Integer insertCSS(StyleChooser insertCSS) {
          Session session = sessionFactory.openSession();
          Transaction tx = session.beginTransaction();
          Integer id = (Integer) session.save(insertCSS);
          session.flush();
          tx.commit();
          return id;
      }
  
      @Transactional
      public void deleteCSS(Integer CSSId) {
          Session session = sessionFactory.openSession();
          Transaction tx = session.beginTransaction();
          StyleChooser sc = (StyleChooser) session.get(StyleChooser.class, CSSId);
          session.delete(sc);
          session.flush();
          tx.commit();
      }
  
      @Transactional
      public void updateCSS(StyleChooser cssWithNewValues) {
          Session session = sessionFactory.openSession();
          Transaction tx = session.beginTransaction();
          session.update(cssWithNewValues);
          session.flush();
          tx.commit();        
      }
  }
  </code></pre>
  
  <p>Accessing DAO...</p>
  
  <pre><code>package net.controllers;
  
  import java.util.List;
  import javax.servlet.http.HttpServletRequest;
  import net.form.StyleChooser;
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.xml.XmlBeanFactory;
  import org.springframework.core.io.FileSystemResource;
  import org.springframework.core.io.Resource;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
  import dao.CSSDAOImpl;
  
  @Controller
  @RequestMapping("/stylechoosertable.html")
  public class IndexController extends MultiActionController {
  
      Resource resource = new FileSystemResource(
              "C:/Users/Steven/Desktop/Programming/workspace/CSSGeneratorHibernate4/WebContent/WEB-INF/hibernate.cfg.xml");
      BeanFactory beanFactory = new XmlBeanFactory(resource);
      CSSDAOImpl dao = (CSSDAOImpl) beanFactory.getBean("CSSDAO");
  
      @RequestMapping(method = RequestMethod.GET)
      public ModelAndView showIndex(HttpServletRequest request) throws Exception {
          List&lt;StyleChooser&gt; styleChooser = dao.selectAllCSS();
          return new ModelAndView("stylechoosertable", "styleChooser", styleChooser);
      }
  }
  </code></pre>