title: java.lang.NullPointerException exception in my controller file (using Spring Hibernate Maven)
body: <p>The problem happens when I run this part (@RequestMapping("/SearchStaff")) in my controller file. I can display my search form successfullt (the request mapping below SearchStaff).</p>
  
  <p>The problem doesn't seemed to have anything to do with Hibernate. As I've commented the Hibernate stuff but I'm still getting it.</p>
  
  <p>If I comment out this line</p>
  
  <pre><code>message = staffDAO.searchForStaff(search);
  </code></pre>
  
  <p>in my controller file, it goes through ok. But I don't see anything wrong with searchForStaff function. It's a very simple function that just returns the string "test" and run system.out.println("test"). </p>
  
  <p>Can you please help? thanks</p>
  
  <hr>
  
  <p><strong>But this is the error that I'm getting:</strong></p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  
  root cause
  
  java.lang.NullPointerException
      org.flinders.staffdirectory.controllers.SearchController.showSearchResults(SearchController.java:25)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:601)
      org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p><strong>My spring-servlet 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:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;context:component-scan base-package="org.flinders.staffdirectory.controllers" /&gt;
  
      &lt;mvc:annotation-driven /&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;tx:annotation-driven /&gt;
  
      &lt;bean
          id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/spring.properties" /&gt;
      &lt;bean
          id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.databaseurl}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" /&gt;
      &lt;bean
          id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configLocation="${hibernate.config}"
          p:packagesToScan="org.flinders.staffdirectory"/&gt;
      &lt;bean
          id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory" /&gt;
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"
          p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" /&gt;
      &lt;bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
          p:definitions="/WEB-INF/tiles.xml" /&gt;
  
      &lt;bean
          id="staffDAO"
          class="org.flinders.staffdirectory.dao.StaffDAO"
          p:sessionFactory-ref="sessionFactory" /&gt;
      &lt;!-- &lt;bean
          id="staffService"
          class="org.flinders.staffdirectory.services.StaffServiceImpl"
          p:staffDAO-ref="staffDAO" /&gt;--&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>This is my controller file</strong></p>
  
  <pre><code>package org.flinders.staffdirectory.controllers;
  
  import java.util.List;
  
  //import org.flinders.staffdirectory.models.database.SearchResult;
  import org.flinders.staffdirectory.models.misc.Search;
  import org.flinders.staffdirectory.dao.StaffDAO;
  
  //import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.servlet.ModelAndView;
  
  @Controller
  public class SearchController {
      //@Autowired
      private StaffDAO staffDAO;
      private String message;
  
      @RequestMapping("/SearchStaff")
      public ModelAndView showSearchResults(@ModelAttribute Search search) {
  
          //List&lt;SearchResult&gt; searchResults =
          message = staffDAO.searchForStaff(search);
  
          //System.out.println(search.getSurname());
          return new ModelAndView("search/SearchForm", "Search", new Search());
          //return new ModelAndView("search/SearchResults", "searchResults", searchResults);
      }
  
      @RequestMapping("/SearchForm")
      public ModelAndView showSearchForm() {
          return new ModelAndView("search/SearchForm", "search", new Search());
      }
  }
  </code></pre>
  
  <p><strong>my dao class</strong></p>
  
  <pre><code>package org.flinders.staffdirectory.dao;
  
  import java.util.List;
  
  import org.hibernate.SessionFactory;
  
  //import org.springframework.beans.factory.annotation.Autowired;
  
  import org.flinders.staffdirectory.models.database.SearchResult;
  import org.flinders.staffdirectory.models.misc.Search;
  
  public class StaffDAO {
      //@Autowired
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      public String searchForStaff(Search search) {
          /*String SQL = "select distinct telsumm_id as id, telsumm_parent_id as parentId, telsumm_name_title as title, (case when substr(telsumm_surname, length(telsumm_surname) - 1, 1) = ',' then substr(telsumm_surname, 1, length(telsumm_surname) - 1) else telsumm_surname end) as surname, telsumm_preferred_name as firstname, nvl(telsumm_tele_number, '-') as telephoneNumber, nvl(telsumm_role, '-') as role, telsumm_display_department as department, lower(telsumm_entity_type) as entityType from teldirt.teld_summary where (telsumm_search_surname is not null) and not (nvl(telsumm_tele_directory,'xxx') IN ('N','S','D')) and not (telsumm_tele_number IS NULL AND telsumm_alias IS NULL) and (telsumm_alias_list = 'Y' OR (telsumm_tele_directory IN ('A','B'))) and ((nvl(telsumm_system_id_end,sysdate+1) &gt; SYSDATE and telsumm_entity_type = 'P') or (telsumm_entity_type = 'N')) and (telsumm_search_department NOT like 'SPONSOR%')";
  
          if (search.getSurname().length() &gt; 0) {
              SQL += " and (telsumm_search_surname like '" + search.getSurname().toUpperCase() + "%')";
          }
          if (search.getSurnameLike().length() &gt; 0) {
              SQL += " and (telsumm_search_soundex like soundex(('%" + search.getSurnameLike().toUpperCase() + "%'))";
          }
          if (search.getFirstname().length() &gt; 0) {
              SQL += " and (telsumm_search_preferred_name like '" + search.getFirstname().toUpperCase() + "%' or telsumm_search_first_name like '" + search.getFirstname() + "%')";
          }
          if (search.getTelephoneNumber().length() &gt; 0) {
              SQL += " and (telsumm_tele_number like '" + search.getTelephoneNumber() + "%')";
          }
          if (search.getDepartment().length() &gt; 0) {
              SQL += " and (telsumm_search_department like '" + search.getDepartment().toUpperCase() + "%')";
          }
          if (search.getRole().length() &gt; 0) {
              SQL += " and (telsumm_search_role like '" + search.getRole().toUpperCase() + "%')";
          }
          SQL += " order by surname, firstname";
  
          List&lt;Object[]&gt; list = (List&lt;Object[]&gt;) sessionFactory.getCurrentSession().createQuery(SQL).list();
  
          for(int j=0;j&lt;list.size();j++){
              Object [] obj= (Object[])list.get(j);
              for(int i=0;i&lt;obj.length;i++)
                  System.out.println(obj[i]);
          }*/
                  System.out.println("test");
               return "test";
      }
  }
  </code></pre>
  
  <p><strong>my pom.xml</strong></p>
  
  <pre><code>&lt;project
      xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.flinders.staffdirectory&lt;/groupId&gt;
  
      &lt;artifactId&gt;directory-maven&lt;/artifactId&gt;
  
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;name&gt;directory-maven Maven Webapp&lt;/name&gt;
  
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
      &lt;properties&gt;
          &lt;org.springframework.version&gt;3.1.3.RELEASE&lt;/org.springframework.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;3.8.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;commons-dbcp&lt;/groupId&gt;
              &lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;
              &lt;version&gt;1.4&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;4.1.8.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;jstl&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;com.oracle&lt;/groupId&gt;
              &lt;artifactId&gt;ojdbc6&lt;/artifactId&gt;
              &lt;version&gt;11.2.0.3&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;1.5.8&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tiles&lt;/groupId&gt;
              &lt;artifactId&gt;tiles-jsp&lt;/artifactId&gt;
              &lt;version&gt;2.2.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;taglibs&lt;/groupId&gt;
              &lt;artifactId&gt;standard&lt;/artifactId&gt;
              &lt;version&gt;1.1.2&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;directory-maven&lt;/finalName&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>