title: java.lang.NullPointerException when i try to persist
body: <p>I'm coding an application in mave,jsf ,spring ,jpa/hibernate , when I try to access a "user" jsp page mapped to the " UserManagedBean" I get an exception:</p>
  
  <pre><code>GRAVE: java.lang.NullPointerException
  javax.faces.el.EvaluationException: java.lang.NullPointerException
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:619)
  Caused by: java.lang.NullPointerException
      at com.java.projet.managed.bean.UserManagedBean.addUser(UserManagedBean.java:47)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
      at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      ... 20 more
  24 oct. 2013 19:54:48 com.sun.faces.lifecycle.InvokeApplicationPhase execute
  ATTENTION: #{userMB.addUser}: java.lang.NullPointerException
  javax.faces.FacesException: #{userMB.addUser}: java.lang.NullPointerException
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:619)
  Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      ... 19 more
  Caused by: java.lang.NullPointerException
      at com.java.projet.managed.bean.UserManagedBean.addUser(UserManagedBean.java:47)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
      at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      ... 20 more
  24 oct. 2013 19:54:48 org.apache.catalina.core.StandardWrapperValve invoke
  GRAVE: "Servlet.service()" pour la servlet Faces Servlet a généré une exception
  java.lang.NullPointerException
      at com.java.projet.managed.bean.UserManagedBean.addUser(UserManagedBean.java:47)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
      at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>Here the code of my UserManagedBean.java</p>
  
  <pre><code>@ManagedBean(name = "userMB")
  @RequestScoped
  public class UserManagedBean implements Serializable {
  
      private static final long serialVersionUID = 1L;
      private static final String SUCCESS = "success";
      private static final String ERROR = "error";
  
      // Spring User Service is injected...
      @ManagedProperty(value = "#{UserService}")
      private IUserService userService;
  
      List&lt;User&gt; userList;
  
      private int id;
      private String name;
      private String surname;
  
      /**
       * Add User
       * 
       * @return String - Response Message
       */
      public String addUser() {
          try {
              User user = new User();
              user.setId(getId());
              user.setName(getName());
              user.setSurname(getSurname());
              userService.addUser(user);
              return SUCCESS;
          } catch (DataAccessException e) {
              e.printStackTrace();
          }
          return ERROR;
      }
  
      /**
       * Reset Fields
       * 
       */
      public void reset() {
          this.setId(0);
          this.setName("");
          this.setSurname("");
      }
  
      /**
       * Get User List
       * 
       * @return List - User List
       */
      public List&lt;User&gt; getUserList() {
          userList = new ArrayList&lt;User&gt;();
          userList.addAll(getUserService().getUsers());
          return userList;
      }
  
      /**
       * Get User Service
       * 
       * @return IUserService - User Service
       */
      public IUserService getUserService() {
          return userService;
      }
  
      /**
       * Set User Service
       * 
       * @param IUserService
       *            - User Service
       */
      public void setUserService(IUserService userService) {
          this.userService = userService;
      }
  
      /**
       * Set User List
       * 
       * @param List
       *            - User List
       */
      public void setUserList(List&lt;User&gt; userList) {
          this.userList = userList;
      }
  
      /**
       * Get User Id
       * 
       * @return int - User Id
       */
      public int getId() {
          return id;
      }
  
      /**
       * Set User Id
       * 
       * @param int - User Id
       */
      public void setId(int id) {
          this.id = id;
      }
  
      /**
       * Get User Name
       * 
       * @return String - User Name
       */
      public String getName() {
          return name;
      }
  
      /**
       * Set User Name
       * 
       * @param String
       *            - User Name
       */
      public void setName(String name) {
          this.name = name;
      }
  
      /**
       * Get User Surname
       * 
       * @return String - User Surname
       */
      public String getSurname() {
          return surname;
      }
  
      /**
       * Set User Surname
       * 
       * @param String
       *            - User Surname
       */
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
  }
  </code></pre>
  
  <p>UserDAO.java</p>
  
  <pre><code>public class UserDAO implements IUserDAO {
  
      @PersistenceContext
      private EntityManager em;
  
      public void addUser(User user) {
          this.em.persist(user);
      }
  
      public void updateUser(User user) {
          this.em.merge(user);
  
      }
  
      public void deleteUser(User user) {
  
          this.em.remove(user);
      }
  
      public User getUserById(int id) {
  
          // List list = em.createQuery("from User where id=?").setParameter(0,
          // id)).list();
          User instance = em.find(User.class, id);
          return instance;
  
      }
  
      public List&lt;User&gt; getUsers() {
          try {
              // process query
              Query query = em.createQuery("select u from User u ");
              List&lt;User&gt; results = (List&lt;User&gt;) query.getResultList();
              // log.trace("find all not deleted users successful");
              return results;
          } catch (RuntimeException re) {
              // log.error("find all not deleted users failed", re);
              return new ArrayList&lt;User&gt;();
          }
      }
  
  }
  </code></pre>
  
  <p>User.java</p>
  
  <pre><code>@Entity
  @Table(name = "User")
  public class User {
      private int id;
      private String name;
      private String surname;
  
      public User() {
      }
  
      public User(int id, String name, String surname) {
  
          this.id = id;
          this.name = name;
          this.surname = surname;
      }
  
      @Id
      @Column(name = "ID", unique = true, nullable = false)
      public int getId() {
          return this.id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      @Column(name = "NAME", unique = true, nullable = false)
      public String getName() {
          return this.name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @Column(name = "SURNAME", unique = true, nullable = false)
      public String getSurname() {
          return this.surname;
      }
  
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
  }
  </code></pre>
  
  <p>UserService.java</p>
  
  <pre><code>@Transactional(readOnly = true)
  public class UserService implements IUserService  {
  
     IUserDAO userDAO;
  
  
      @Transactional(readOnly = false)
      public void addUser(User user) {
          // TODO Auto-generated method stub
           getUserDAO().addUser(user);
  
      }
  
       @Transactional(readOnly = false)
      public void updateUser(User user) {
          // TODO Auto-generated method stub
           getUserDAO().deleteUser(user);
  
      }
  
       @Transactional(readOnly = false)
      public void deleteUser(User user) {
           getUserDAO().updateUser(user);
  
      }
  
      public User getUserById(int id) {
          // TODO Auto-generated method stub
          return  getUserDAO().getUserById(id);
      }
  
      public List&lt;User&gt; getUsers() {
          return  getUserDAO().getUsers();
      }
  
      public IUserDAO getUserDAO() {
          return userDAO;
      }
  
      public void setUserDAO(IUserDAO userDAO) {
          this.userDAO = userDAO;
      }
  
  
  }
  </code></pre>
  
  <p>db.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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context" 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.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
      default-autowire="byName"&gt;
  
      &lt;!-- Scans within the base package of the application for @Components to 
          configure as beans --&gt;
      &lt;bean id="placeholderConfig"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location" value="classpath:db.properties" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="true" /&gt;
                  &lt;property name="generateDdl" value="true" /&gt;
                  &lt;property name="databasePlatform" value="${db.dialect}" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="${db.driver}" /&gt;
          &lt;property name="url" value="${db.url}" /&gt;
          &lt;property name="username" value="${db.username}" /&gt;
          &lt;property name="password" value="${db.password}" /&gt;
      &lt;/bean&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven /&gt;
      &lt;bean
          class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!-- Use this definition if using a Java EE 6 container This also stops Eclipse 
      from complaining that 3.0 is not a valid version &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" 
      http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt; --&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_2_5.xsd"
      version="2.5"&gt;
  
      &lt;!-- The definition of the Root Spring Container shared by all Servlets 
          and Filters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/app/servlet-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/spring/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;facelets.DEVELOPMENT&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Welcome Page --&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;/pages/index.html&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&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;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.faces&lt;/url-pattern&gt;
        &lt;/servlet-mapping&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
  &lt;/web-app&gt;
  </code></pre>