title: Java+Spring: SEVERE Servletservice
body: <p>I dont know what is wrong in my <code>listContacts</code> in controller
  <code>map.put("contactList", contactService.listContact());</code>
  Can somebody help me?</p>
  
  <blockquote>
    <p>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/test] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
    java.lang.NullPointerException
        at pl.ivmx.contact.controller.ContactController.listContacts(ContactController.java:26)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)</p>
  </blockquote>
  
  <pre><code>package pl.ivmx.contact.controller;
  
  import java.util.Map;    
  import pl.ivmx.contact.dao.ContactDAO;
  import pl.ivmx.contact.form.Contact;
  import pl.ivmx.contact.service.ContactService;    
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  @Controller
  public class ContactController {
  
      @Autowired
      private ContactService contactService;
  
      @RequestMapping("/contact")
      public String listContacts(Map&lt;String, Object&gt; map) {
          map.put("contact", new Contact());
          map.put("contactList", contactService.listContact());       
  
          return "/contact";
      }
  
      @RequestMapping(value = "/add", method = RequestMethod.POST)
      public String addContact(@ModelAttribute("contact") Contact contact,
              BindingResult result) {
  
          contactService.addContact(contact);
  
          return "redirect:/contact";
      }
  
      @RequestMapping("/delete/{contactId}")
      public String deleteContact(@PathVariable("contactId") Integer contactId) {
  
          contactService.removeContact(contactId);
  
          return "redirect:/contact";
      }   
  }
  </code></pre>
  
  <hr>
  
  <pre><code>package pl.ivmx.contact.dao;
  
  import java.util.List;
  
  import pl.ivmx.contact.form.Contact;
  
  public interface ContactDAO {
  
      public void addContact(Contact contact);
      public List&lt;Contact&gt; listContact();
      public void removeContact(Integer id);
  }
  </code></pre>
  
  <hr>
  
  <p>package pl.ivmx.contact.dao;</p>
  
  <pre><code>import java.util.List;
  
  import pl.ivmx.contact.form.Contact;
  
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  
  import org.springframework.stereotype.Repository;
  
  @Repository
  public class ContactDAOImpl implements ContactDAO {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public void addContact(Contact contact) {
          sessionFactory.getCurrentSession().save(contact);
      }
  
      public List&lt;Contact&gt; listContact() { 
          return  sessionFactory.getCurrentSession().createQuery("from Contact").list();      
      }
  
      public void removeContact(Integer id) {
          Contact contact = (Contact) sessionFactory.getCurrentSession().load(
                  Contact.class, id);
          if (null != contact) {
              sessionFactory.getCurrentSession().delete(contact);
          }
  
      }
  }
  </code></pre>
  
  <hr>
  
  <p>package pl.ivmx.contact.service;</p>
  
  <pre><code>import java.util.List;
  
  import pl.ivmx.contact.form.Contact;
  
  public interface ContactService {
  
      public void addContact(Contact contact);
      public List&lt;Contact&gt; listContact();
      public void removeContact(Integer id);
  }
  </code></pre>
  
  <hr>
  
  <p>package pl.ivmx.contact.service;</p>
  
  <pre><code>import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import pl.ivmx.contact.dao.ContactDAO;
  import pl.ivmx.contact.form.Contact;
  
  @Service
  public class ContactServiceImpl implements ContactService {
  
      @Autowired
      private ContactDAO contactDAO;
  
      @Transactional
      public void addContact(Contact contact) {
          contactDAO.addContact(contact);
      }
  
      @Transactional
      public List&lt;Contact&gt; listContact() {
          return contactDAO.listContact();
      }
  
      @Transactional
      public void removeContact(Integer id) {
          contactDAO.removeContact(id);
      }  
  }
  </code></pre>
  
  <hr>
  
  <p>package pl.ivmx.contact.form;</p>
  
  <pre><code>import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="CONTACTS")
  public class Contact {
  
      @Id
      @Column(name="ID")
      @GeneratedValue
      private Integer id;
  
      @Column(name="FIRSTNAME")
      private String firstname;
  
      @Column(name="LASTNAME")
      private String lastname;
  
      @Column(name="EMAIL")
      private String email;
  
      @Column(name="TELEPHONE")
      private String telephone;
  
  
      public String getEmail() {
          return email;
      }
      public String getTelephone() {
          return telephone;
      }
      public void setEmail(String email) {
          this.email = email;
      }
      public void setTelephone(String telephone) {
          this.telephone = telephone;
      }
      public String getFirstname() {
          return firstname;
      }
      public String getLastname() {
          return lastname;
      }
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
      public Integer getId() {
          return id;
      }
      public void setId(Integer id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0"&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;WEB-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt; 
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;/pages/index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  </code></pre>
  
  <h2>    </h2>
  
  <p>applicationContext.xml</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:security="http://www.springframework.org/schema/security"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/security
          http://www.springframework.org/schema/security/spring-security-3.0.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="pl.ivmx.contact" /&gt;  
  
  &lt;!--    &lt;bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="messages_en.properties" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8" /&gt;
      &lt;/bean&gt; --&gt;
  
  
      &lt;import resource="commonContext.xml" /&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
          &lt;property name="url" value="${jdbc.url}" /&gt;
          &lt;property name="username" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="userTestDao" class="pl.ivmx.dao.impl.UserTestDaoImpl"&gt;
      &lt;!--     &lt;property name="dataSource" ref="dataSource" /&gt;   --&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;             
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" &gt;
  
      &lt;!--    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; --&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;                 
           &lt;property name="configLocation" value="META-INF/hibernate.cfg.xml" /&gt;    
          &lt;property name="configurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
      &lt;!--    &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&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;pl.ivmx.model.UserTest&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;    --&gt;     
      &lt;/bean&gt;    
  
  
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <h2>    </h2>
  
  <p>dispatcher-servlet.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:tx="http://www.springframework.org/schema/tx"                                 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.1.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;!--  &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /&gt;    --&gt;
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
  
          &lt;property name="urlMap"&gt;
              &lt;map&gt;
                  &lt;entry key="/index.do"&gt; &lt;ref bean="index" /&gt;&lt;/entry&gt;
                  &lt;entry key="/registration.do"&gt; &lt;ref bean="registration" /&gt;&lt;/entry&gt;
                  &lt;entry key="/usertestlist.do"&gt; &lt;ref bean="usertest" /&gt;&lt;/entry&gt;  
                  &lt;entry key="/contact.do"&gt; &lt;ref bean="contact" /&gt;&lt;/entry&gt;        
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="index" class="pl.ivmx.web.IndexController"/&gt;
  
          &lt;bean id="registrationValidator" class="pl.ivmx.validation.RegistrationValidator" /&gt;  
          &lt;bean id="registration" class="pl.ivmx.web.RegistrationFormController" &gt;                 
              &lt;property name="commandName"&gt;&lt;value&gt;userTest&lt;/value&gt;&lt;/property&gt; 
              &lt;property name="commandClass"&gt;&lt;value&gt;pl.ivmx.model.UserTest&lt;/value&gt;&lt;/property&gt; 
              &lt;property name="validator"&gt;&lt;ref local="registrationValidator"/&gt;&lt;/property&gt;  
              &lt;property name="formView"&gt;&lt;value&gt;registration&lt;/value&gt;&lt;/property&gt; 
              &lt;property name="successView"&gt;&lt;value&gt;registrationsuccess&lt;/value&gt;&lt;/property&gt; 
              &lt;property name="userTestDao"&gt;&lt;ref bean="userTestDao"/&gt;&lt;/property&gt;               
          &lt;/bean&gt;     
  
          &lt;bean id="usertest" class="pl.ivmx.web.UserTestController"&gt;                  
               &lt;property name="userTestDao"&gt;&lt;ref bean="userTestDao"/&gt;&lt;/property&gt;          
          &lt;/bean&gt;     
  
      &lt;bean id="contact" class="pl.ivmx.contact.controller.ContactController"&gt;
      &lt;/bean&gt;
  
      &lt;bean id="contactService" class="pl.ivmx.contact.service.ContactServiceImpl"&gt;
      &lt;/bean&gt;
      &lt;bean id="contactDAO" class="pl.ivmx.contact.dao.ContactDAOImpl"&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>