title: EJB not getting injected resulting in NullPointerException
body: <p>I am new to Java EE and have been trying to resolve this issue for almost a week.
  This is the code in my session bean where in I am getting a <code>NullPointerException</code>. Found out that the entity manager is not getting injected.</p>
  
  <p>The code from the session bean:</p>
  
  <pre><code>package session;
  
  import entity.User;
  import java.util.Iterator;
  import java.util.ArrayList;
  import java.util.List;
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  @Stateless
  public class UserFacade extends AbstractFacade&lt;User&gt; {
      @PersistenceContext (unitName = "EXAMPU")
      private EntityManager em;
  
      @Override
      protected EntityManager getEntityManager() {
      return em;
      }
  
      public UserFacade() {
      super(User.class);
      }
  
      public String validateUser(String username, String password) {
      System.out.println("in facade");    
      boolean pwdMatch = false;
  
      System.out.println("in UserInfoFacade validate " + em);
      try {
          System.out.println("username " + username);
          System.out.println("password " + password);
  
          if(em == null) {
          System.out.println("em is null");
          }
          else {
          System.out.println("em is not null");
          }
  
          List results = em.createNamedQuery("User.findByUserName").setParameter("userName", username).getResultList();
  
          if(results == null) {
          System.out.println("results is null");
          }
          else {
          System.out.println("results is not null");
          }
          System.out.println("Entity manger is open " + em.isOpen());
  
          if (!results.isEmpty())
          {
  
              Iterator userList = results.iterator();
              while (userList.hasNext())
              {
              User user = (User) userList.next();
              if (user != null)
              {
                  if (user.getPassword().equals(password))
                  {
                  pwdMatch = true;
                  }
                  else {
                  pwdMatch = false;
                  }
              }
              else {
                  return "NULL";
              }
              }
              if (pwdMatch) {
              return "MATCH";
              }
              else {
              return "NO_MATCH";
              }
          }
          else {
          return "NOT_FOUND";
          }
      } catch (Exception ex) {
          //ex.printStackTrace();
          System.out.println("Caught exception in UserFacade " + ex.toString());
      }
      return "NULL";
      }
  
  
  }
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
    &lt;persistence-unit name="EXAMPU" transaction-type="JTA"&gt;
        &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt; 
        &lt;jta-data-source&gt;EXAM&lt;/jta-data-source&gt;
  
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties/&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>code for managed bean : userBean</p>
  
  <pre><code>package managed;
  
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  import session.UserFacade;
  import javax.ejb.EJB;
  
  @ManagedBean
  @SessionScoped
  public class userBean {
  
      private String username, password;
      private String response="" ;
      private UserFacade userFacade;
  
      public userBean() {
          this.userFacade = new UserFacade();
      }
  
      public String getUsername() {
          return username;
      }
  
      public String getPassword() {
          return password;
      }
  
      public String getResponse() {
          return response;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public void setResponse(String response) {
          this.response = response;
      }
  
      public String validate() {
          System.out.println("in validate going to user facade" + username + password);
          response = userFacade.validateUser(username,password);
          System.out.println("validUser value  " + response);
          if (response.equals("MATCH")){           
              return "welcome.xhtml";
           }
          else
              return "index.xhtml";
          }
       }
  }
  </code></pre>