title: JSF simple entity persistence from a form
body: <p>I think that the question is simple, I get the error:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
  at co.edu.unal.bienestar.dao.UserDao.save(UserDao.java:27)
  at co.edu.unal.bienestar.facade.UserFacade.createUser(UserFacade.java:18)
  at co.edu.unal.bienestar.mb.UserMB.createUser(UserMB.java:40)
  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.apache.el.parser.AstValue.invoke(AstValue.java:262)
  at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
  at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
  at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
  ... 27 more
  </code></pre>
  
  <p>When I try the create a new user, with a application which only have a entity class:</p>
  
  <pre><code>@Entity
  public class User implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private String username;
      private String password;
      private Role role;
  
      &lt;here getters and setters&gt;
  }
  </code></pre>
  
  <p>a Data Access Object:</p>
  
  <pre><code>public class UserDao {
  
      @PersistenceContext
      private EntityManager em;
  
  
      public void save(User user) {
          em.persist(user);
      }
  
      public void delete(User user) {
          User userToBeRemoved = em.merge(user);
          em.remove(userToBeRemoved);
      }
  
      public User update(User user) {
          return em.merge(user);
      }
  
      public List&lt;User&gt; findAll() {
          TypedQuery&lt;User&gt; query = em.createQuery(
                  "SELECT u FROM User u ORDER BY u.id", User.class);
          return query.getResultList();
      }
  }
  </code></pre>
  
  <p>a facade:</p>
  
  <pre><code>public class UserFacade {
  
      private UserDao userDao = new UserDao();
  
      public void createUser(User user) {
          userDao.save(user);
      }
  
      public List&lt;User&gt; listAll() {
          List&lt;User&gt; result = userDao.findAll();
          return result;
      }
  }
  </code></pre>
  
  <p>User Managed Bean:</p>
  
  <pre><code>@SessionScoped
  @ManagedBean(name = "userMB")
  public class UserMB implements Serializable {
      public static final String INJECTION_NAME = "#{userMB}";
      private static final long serialVersionUID = 1L;
      private User user;
      private UserFacade userfacade;
  
  
      public User getUser() {
          if (user == null) {
              user = new User();
          }
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public void createUser() {
          getUserfacade().createUser(user);
      }
  
      public UserFacade getUserfacade() {
          if (userfacade == null){
              userfacade = new UserFacade();
          }
          return userfacade;
      }
  }
  </code></pre>
  
  <p>And finally from JSF page I call the method like this:</p>
  
  <pre><code>    &lt;f:view&gt;
          &lt;h:form&gt;
              &lt;h1&gt;&lt;h:outputText value="Create/Edit"/&gt;&lt;/h1&gt;
              &lt;h:panelGrid columns="2"&gt;
                  &lt;h:outputLabel value="Username:"/&gt;
                  &lt;h:inputText id="username" value="#{userMB.user.username}" title="username" /&gt;
                  &lt;h:outputLabel value="Password:" /&gt;
                  &lt;h:inputText id="password" value="#{userMB.user.password}" title="password" /&gt;
                     &lt;h:outputLabel value="ID:" /&gt;
                     &lt;h:inputText id="id" value="#{userMB.user.id}" title="id" /&gt;                                      
              &lt;/h:panelGrid&gt;
              &lt;h:commandButton action="#{userMB.createUser}" value="create"/&gt;
          &lt;/h:form&gt;
      &lt;/f:view&gt;
  </code></pre>
  
  <p>Where is my mistake?</p>