title: EntityManager NullPointerException
body: <p>Yet another 'EntityManager is null' question. I've searched for an answer but haven't found one that works for me. I'm taking over a large project and I'm trying to simplify it to cut out red herrings when debugging. I'm down to a simple Customer/Order system and I'm just trying to deploy it and view the list of customers. Based on another question I asked and a <a href="https://stackoverflow.com/a/24046941/3337410">resulting suggestion</a> I'm down to the following (actual question and stack trace at bottom):</p>
  
  <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="my_PU" transaction-type="JTA"&gt;
        &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;jta-data-source&gt;my_datasource&lt;/jta-data-source&gt;
          &lt;class&gt;my.database.model.Customer&lt;/class&gt;
          &lt;class&gt;my.database.model.Order&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <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;display-name&gt;MyWebApp&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&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;/faces/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;context-param&gt;
      &lt;description&gt;State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2&lt;/description&gt;
      &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt;
      &lt;param-value&gt;client&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;javax.servlet.jsp.jstl.fmt.localizationContext&lt;/param-name&gt;
      &lt;param-value&gt;resources.application&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.FACELETS_LIBRARIES&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/balusc.taglib.xml&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL&lt;/param-name&gt;
      &lt;param-value&gt;true&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;filter&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.primefaces.webapp.filter.FileUploadFilter&lt;/filter-class&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
    &lt;/filter-mapping&gt;
    &lt;context-param&gt;
      &lt;param-name&gt;javax.faces.PARTIAL_STATE_SAVING&lt;/param-name&gt;
      &lt;param-value&gt;false&lt;/param-value&gt;
    &lt;/context-param&gt;
    &lt;listener&gt;
      &lt;listener-class&gt;com.sun.faces.config.ConfigureListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>webpage - menu.xhtml</p>
  
  <pre><code>&lt;html
   xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"&gt;
  &lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" media="screen,projection"
      href="../css/menu.css" /&gt;&lt;/head&gt;
  &lt;ui:component&gt;
      &lt;div class="menu-container"&gt;
          &lt;div class="menu-tab"&gt;
              &lt;ul&gt;
                  &lt;li&gt;&lt;a href="index.xhtml"&gt;&lt;span&gt;Home&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
  
                  &lt;li&gt;&lt;h:outputLink
                          value="#{customerController.getUrl()}"&gt;
                          &lt;span&gt;&lt;h:outputText value="Customer" /&gt;&lt;/span&gt;
                      &lt;/h:outputLink&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;&lt;span&gt;Logout&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/ui:component&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>CustomerController.java</p>
  
  <pre><code>package my.webapp.web.controller;
  
  import java.util.List;
  import javax.annotation.PostConstruct;
  import javax.enterprise.context.RequestScoped;
  import javax.inject.Named;
  import my.model.Customer;
  
  @Named(value = "customerController")
  @RequestScoped
  public class CustomerController// extends FormRequestController
  {
      private Customer customer;
      private String id;
      protected List&lt;?&gt; entityObjectList;
      protected boolean editMode;
  
      @PostConstruct
      public void init() {
          System.out.println("CustomerController init");
          setEntityObjectList(findAll());
      }
  
      public List&lt;?&gt; findAll() {
          List&lt;Customer&gt; dataList = Customer.getAllRecords();
          return dataList;
  
      }
  
      public String getUrl()
      {
          System.out.println("CustomerController getUrl");
          return "www.google.com";
      }
      //getters and setters ommitted for brevity
  }
  </code></pre>
  
  <p>Customer.java</p>
  
  <pre><code>package my.model;
  
  import java.io.Serializable;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import javax.ejb.EJB;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import my.database.manager.PersistenceStoreManager;
  
  @Entity
  @Table(name=Customer.TBL_NAME)
  @NamedQueries({
      @NamedQuery(name=Customer.QRY_ALL, query="select object(a) from Customer a")
  })
  public class Customer implements Serializable {
      private static final long serialVersionUID = 1L;
  
      /**
       * Manager class to manager DB connections
       */
      @EJB
      private static PersistenceStoreManager&lt;Customer&gt; psm;
      // Table constants
      public static final String TBL_NAME = "Customer";
      public static final String QRY_ALL = TBL_NAME + ".all";
  
      private int id;
      private String name;
      private Set&lt;Order&gt; orders;
  
      public Customer() {
      }
  
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public int getId() {
          return this.id;
      }
  
      //bi-directional many-to-one association to Order
      @OneToMany(mappedBy="id")
      public Set&lt;Order&gt; getOrders() {
          return this.orders;
      }
  
      public void setOrders(Set&lt;Order&gt; orders) {
          this.orders = orders;
      }
  
      public static List&lt;Customer&gt; getAllRecords(){
          Map&lt;String, Object&gt; params = new HashMap&lt;String, Object&gt;();
          return psm.getResultSetList(QRY_ALL, params);
      }
      //other getters and setters omitted for brevity
  }
  </code></pre>
  
  <p>PersistenceStoreManager.java</p>
  
  <pre><code>package my.database.manager;
  
  import java.util.List;
  import java.util.Map;
  import javax.annotation.PostConstruct;
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.Query;
  
  /**
   * This class is responsible for performing CRUD operations using the
   * EntityManager of the PersistenceContext.
   *
   *
   */
  @Stateless
  public class PersistenceStoreManager&lt;T&gt;
  {
       @PersistenceContext(unitName = "my_PU")
      private EntityManager em;
  
  
      public PersistenceStoreManager ()
      {
          System.out.println("PersistenceStoreManager loaded");
          System.out.println("em is null: "+(em==null));
      }
  
      @PostConstruct
      public void postInject()
      {
          System.out.println("postInject");
          System.out.println("em is null: "+(em==null));
      }
  
      /**
       * @param namedQuery
       * @param criteria
       * @return
       */
      public List&lt;T&gt; getResultSetList(String namedQuery,
              Map&lt;String, Object&gt; criteria) {
  
          Query q = em.createNamedQuery(namedQuery);
          for (Map.Entry&lt;String, Object&gt; entry : criteria.entrySet()) {
              q.setParameter(entry.getKey(), entry.getValue());
          }
          List&lt;T&gt; resultSetList = q.getResultList();
          return resultSetList;
      }
  }
  </code></pre>
  
  <p>My Application deploys but I get the ubiquitous <code>NullPointerException</code> when trying to view the <code>menu.xhtml</code> page. The following is logged when I deploy my application:</p>
  
  <blockquote>
    <p>-WARNING|org.apache.catalina.connector.Request|_ThreadID=37;_ThreadName=Thread-2;|PWC4011: Unable to set request character encoding to UTF-8 from context , because request parameters have already been read, or ServletRequest.getReader() has already been called|#]<br>
    -INFO|uploadFileName=MyWebApp-0.0.1-SNAPSHOT.war|#]<br>
    -INFO|javax.enterprise.system.container.ejb.com.sun.ejb.containers|Portable JNDI names for EJB PersistenceStoreManager: [java:global/MyWebApp-0.0.1-SNAPSHOT/PersistenceStoreManager, java:global/MyWebApp-0.0.1-SNAPSHOT/PersistenceStoreManager!my.database.manager.PersistenceStoreManager]|#]<br>
    -INFO|javax.enterprise.system.std.com.sun.enterprise.server.logging|<strong>PersistenceStoreManager loaded</strong>|#]<br>
    -INFO|javax.enterprise.system.std.com.sun.enterprise.server.logging|<strong>em is null: true</strong>|#]<br>
    -INFO|javax.enterprise.resource.webcontainer.jsf.config|Initializing Mojarra 2.1.6 (SNAPSHOT 20111206) for context '/MyWebApp-0.0.1-SNAPSHOT'|#]<br>
    -INFO|org.primefaces.webapp.PostConstructApplicationEventListener|Running on PrimeFaces 3.4.1|#]<br>
    -INFO|javax.enterprise.system.container.web.com.sun.enterprise.web|WEB0671: Loading application [MyWebApp-0.0.1-SNAPSHOT] at [/MyWebApp-0.0.1-SNAPSH   6243 OT]|#]<br>
    -INFO|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|MyWebApp-0.0.1-SNAPSHOT was successfully deployed in 1,598 milliseconds.|#]</p>
  </blockquote>
  
  <p>When I navigate to myserver:8080/MyWebApp-0.0.1-SNAPSHOT/faces/index.xhtml The following is logged:</p>
  
  <blockquote>
    <p>[#|2014-06-06T16:51:56.988+0100|SEVERE|glassfish3.1.2|javax.enterprise.resource.webcontainer.jsf.application|_ThreadID=36;_ThreadName=Thread-2;|Error Rendering View[/index.xhtml]<br>
    javax.el.ELException: /templates/menu.xhtml @15,64 value="#{customerController.getUrl()}": org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] @PostConstruct public my.webapp.web.controller.CustomerController.init() on my.webapp.web.controller.CustomerController@4085f7ff<br>
     at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)<br>
     at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)<br>
     ...[long stack trace]...<br>
    Caused by: java.lang.NullPointerException<br>
     at my.model.Customer.getAllRecords(Customer.java:64)<br>
     at my.webapp.web.controller.CustomerController.findAll(CustomerController.java:41)<br>
     at my.webapp.web.controller.CustomerController.init(CustomerController.java:37)<br>
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br>
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)<br>
     ...[long stack trace]...</p>
  </blockquote>
  
  <p>None of the logging in the <code>postInject()</code> methods in <code>Customer</code>, <code>CustomerController</code> and <code>PersistenceStoreManager</code> is being output. Since they are annotated with <code>@PostConstruct</code> and should be called after injection, I assume that the EJB injection isn't taking place.</p>
  
  <p>Can anyone see why the EJB injection isn't happening?</p>