title: Hibernate, Spring, Dao getSession() NullPointerException
body: <p>I get the error.</p>
  
  <pre><code>java.lang.NullPointerException
  at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:143)
  at com.walladverts.model.dao.UserDao.findByUsername(UserDao.java:25)
  </code></pre>
  
  <p>while accessing getSession() in findByUsername</p>
  
  <pre><code>package com.walladverts.model.dao;
  import java.util.List;
  import org.hibernate.Criteria;
  import org.hibernate.Session;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.stereotype.Repository;
  import com.walladverts.exceptions.DataNotFoundException;
  import com.walladverts.model.entities.User;
  import com.walladverts.util.CustomHibernateDaoSupport;@
  Repository("userDao")
  public class UserDao extends CustomHibernateDaoSupport {
      public void save(User user) {
          getHibernateTemplate().save(user);
      }
      public void delete(User user) {
          getHibernateTemplate().delete(user);
      }
      public User findByUsername(String username) throws DataNotFoundException {
          Session session = getSession();
          Criteria crit = session.createCriteria(User.class);
          System.out.println(username);
          crit.add(Restrictions.eq("username", username));
          crit.setMaxResults(1);
          List &lt; User &gt; users = crit.list();
          System.out.println(users);
          if (users.size() &lt; 1) {
              throw new DataNotFoundException();
          }
          return users.get(0);
      }
  }
  </code></pre>
  
  <p>Parent class:</p>
  
  <pre><code>package com.walladverts.util;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport {@
      Autowired
      public void init(SessionFactory factory) {
          setSessionFactory(factory);
      }
  }
  </code></pre>
  
  <p><strong>Does anybody has an idea why this occurs? It freezes me for developing.</strong></p>
  
  <p>EDIT:</p>
  
  <p>It happens when Spring Secure tries to sign in an user. Also my SessionFactory is working OK when calling this method from Controller.</p>