title: NullPointerException at sessionFactory using spring
body: <p>I am facing this problem for several days now and tried - in my opinion - everything to find a solution to this. I am completely out of ideas...</p>
  
  <p>Desciption of what the program should do: It should, after entering a username and password, connect to a database and check if the entered username/password is in that database. Due to the NullPointer of <strong>sessionFactory</strong> he never gets into this check.</p>
  
  <p>Stacktrace:</p>
  
  <pre><code>java.lang.NullPointerException
  at de.mail.HibernateUtil.getSession(HibernateUtil.java:37)
  at de.mail.gui.LoginServlet.doPost(LoginServlet.java:56)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:744)
  </code></pre>
  
  <p>context.xml:</p>
  
  <pre><code>    &lt;!-- Component Scans --&gt;
  &lt;context:component-scan base-package="de.mail" use-default-filters="false"&gt;
      &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /&gt;
  &lt;/context:component-scan&gt;
  
  &lt;!-- Datenbank --&gt;
  &lt;bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClass" value="org.postgresql.Driver" /&gt;
      &lt;property name="jdbcUrl"
          value="****" /&gt;
      &lt;property name="maxPoolSize" value="1" /&gt;
      &lt;property name="properties"&gt;
          &lt;props&gt;
              &lt;prop key="user"&gt;postgres&lt;/prop&gt;
              &lt;prop key="password"&gt;****&lt;/prop&gt;
              &lt;prop key="characterEncoding"&gt;UTF-8&lt;/prop&gt;
              &lt;!-- Properties sind nur notwendig, wenn man mal SSL für die DB-Verbindung 
                  braucht (auch in der properties entkommentieren) --&gt;
              &lt;!-- &lt;prop key="ssl"&gt;${db.ssl}&lt;/prop&gt; &lt;prop key="sslfactory"&gt;${db.sslfactory}&lt;/prop&gt; --&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;alias name="c3p0DataSource" alias="dataSource" /&gt;
  
  &lt;!-- TransactionManager --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;property name="nestedTransactionAllowed" value="true" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- SessionFactory --&gt;
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      destroy-method="destroy"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan"&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;
              &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
      &lt;property name="annotatedPackages"&gt;
          &lt;list&gt;
              &lt;value&gt;de.mail&lt;/value&gt;
              &lt;value&gt;de.mail.data.dao&lt;/value&gt;
              &lt;value&gt;de.mail.vo&lt;/value&gt;
              &lt;value&gt;de.mail.gui&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;de.mail.data.vo.UserVO&lt;/value&gt;
              &lt;value&gt;de.mail.data.vo.FilterVO&lt;/value&gt;
              &lt;value&gt;de.mail.data.vo.ResultVO&lt;/value&gt;
              &lt;value&gt;de.mail.data.dao.ActionDAO&lt;/value&gt;
              &lt;value&gt;de.mail.HibernateUtil&lt;/value&gt;
              &lt;value&gt;de.mail.FilterJob&lt;/value&gt;
              &lt;value&gt;de.mail.gui.ActionServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.CreateServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.DeleteServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.EditServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.FormServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.InitServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.LoginServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.LogoutServlet&lt;/value&gt;
              &lt;value&gt;de.mail.gui.RegistrationServlet&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>HibernateUtil:</p>
  
  <pre><code>public abstract class HibernateUtil extends HttpServlet{
  
  private static final long serialVersionUID = -1850678939342717642L;
  
  @Autowired
  private SessionFactory sessionFactory;
  
  /**
   * Gibt die derzeitige Session zurück
   * 
   * @return derzeitige Session
   * @throws HibernateException
   */
  public Session getSession() {
      /*try {
          return sessionFactory.getCurrentSession();          
      } catch (Throwable e){
          System.out.println("---&gt; " + e);
  
          return null;
      }*/
      return sessionFactory.getCurrentSession();
  }
  </code></pre>
  
  <p>LoginServlet:</p>
  
  <pre><code>@Component
  public class LoginServlet extends HibernateUtil {
  
  /**
   * 
   */
  private static final long serialVersionUID = -776218596462464850L;
  private static final Log LOG = LogFactory.getLog(LoginServlet.class);
  private static final int HEX_FF = 0xFF;
  
  /**
   * Bearbeitet den Loginversuch&lt;br /&gt;
   * Es werden die eingegebenen Daten ausgelesen. Aus dem Passwort wird ein Hash-Wert generiert, der mit dem Datenbankeintrag mit dem
   * passenden Usernamen verglichen wird. Stimmt alles &amp;uumlberein wird auf die main.jsp weitergeleitet. Im Fehlerfall wird zur&amp;uumlck auf
   * die login.jsp verwiesen.
   * 
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   * @param response der HttpResponse
   * @param request der HttpRequest
   * @throws ServletException ex
   * @throws IOException ex
   */
  @Override
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
  
      if (!"Registrieren".equals(request.getParameter("register"))) {
  
          final Session session = getSession();
  
          // Generiert einen HashWert aus dem eingegebenen Passwort
          try {
              final byte[] pwBytes = request.getParameter("password").getBytes();
              final MessageDigest algorithm = MessageDigest.getInstance("MD5");
              algorithm.reset();
              algorithm.update(pwBytes);
              final byte[] messageDigest = algorithm.digest();
  
              final StringBuffer hexString = new StringBuffer();
  
              for (byte digest : messageDigest) {
                  hexString.append(Integer.toHexString(HEX_FF &amp; digest));
              }
  
              final LoginDAO dao = new LoginDAO();
              final List&lt;UserVO&gt; list = dao.checkLogin(request, session);
  
              final UserVO userVO = list.get(0);
  
              if (userVO.getPassword().equals(hexString.toString())) {
  
                  request.getSession().setAttribute("userid", userVO.getId());
                  final RequestDispatcher dispatcher = request.getRequestDispatcher("main.jsp");
                  dispatcher.forward(request, response);
  
              } else {
  
                  final RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
                  dispatcher.forward(request, response);
              }
  
          } catch (NoSuchAlgorithmException nsax) {
              LOG.debug("Keine MessageDigestSpi fuer den entsprechenden Alogrithmus gefunden");
  
          } catch (NullPointerException npx) {
              final RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
              dispatcher.forward(request, response);
  
          } catch (IndexOutOfBoundsException ioobx) {
              final RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp");
              dispatcher.forward(request, response);
          }
      } else {
          final RequestDispatcher dispatcher = request.getRequestDispatcher("register.jsp");
          dispatcher.forward(request, response);
      }
  }
  
  /**
   * Tut nix besonders. Leiet an die doPost-Methode weiter
   * 
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   * @param response der HttpResponse
   * @param request der HttpRequest
   * @throws ServletException ex
   * @throws IOException ex
   */
  @Override
  protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
      doPost(request, response);
  }
  </code></pre>
  
  <p>I really don't get the error here. I checked every file, especially the spring context for missing content but found really nothing. I assume that the error is caused by something pretty simple. For me - I am pretty new to spring and hibernate - the error doesn't want to be found.</p>