title: Java EntityManager throwing NullPointerException
body: <p>When I try to run the Unit test for this DAO class, I am getting NullPointerException at getById return statement. I know that the class does not initialize the EntityManager, but I don’t understand why? - I can’t tell whether my persistence.xml configuration is wrong or the DB credentials are incorrect.</p>
  <p>I saw two or more StackOverflow threads but had had little luck. I am using Intellij IDE.</p>
  <pre><code>package com.beetlehand.model.dao;
  
  import com.beetlehand.model.AttributeEntity;
  import org.apache.commons.lang.StringUtils;
  
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.TypedQuery;
  import java.util.List;
  import java.util.Map;
  import java.util.ArrayList;
  
  @Stateless
  public class AttributeDao extends AbstractDao&lt;AttributeEntity&gt; {
  
      @PersistenceContext(unitName = &quot;NewPersistenceUnit&quot;)
      protected EntityManager entityManager;
  
      public AttributeEntity getById(Long id) {
          if(id == null) return null;
          return entityManager.find(AttributeEntity.class, id);
      }
  
      /*** more code ***/ 
  }
  </code></pre>
  <p>Persistence configuration file</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot; version=&quot;2.0&quot;&gt;
  
      &lt;persistence-unit name=&quot;NewPersistenceUnit&quot;&gt;
          &lt;class&gt;com.beetlehand.model.AttributeEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.AttributeValueEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.AuditEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.CategoryEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.CustomerEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.DepartmentEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.OrderDetailEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.OrdersEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ProductEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ProductAttributeEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ProductCategoryEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ReviewEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ShippingEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ShippingRegionEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.ShoppingCartEntity&lt;/class&gt;
          &lt;class&gt;com.beetlehand.model.TaxEntity&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name=&quot;toplink.jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/beetlehand&quot;/&gt;
              &lt;property name=&quot;toplink.jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;
              &lt;property name=&quot;hibernate.connection.url&quot; value=&quot;jdbc:mysql://localhost:3306/beetlehand&quot;/&gt;
              &lt;property name=&quot;hibernate.connection.driver_class&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;
              &lt;property name=&quot;openjpa.ConnectionURL&quot; value=&quot;jdbc:mysql://localhost:3306/beetlehand&quot;/&gt;
              &lt;property name=&quot;openjpa.ConnectionDriverName&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;
              &lt;property name=&quot;eclipselink.jdbc.url&quot; value=&quot;jdbc:mysql://localhost:3306/beetlehand&quot;/&gt;
              &lt;property name=&quot;eclipselink.jdbc.driver&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;
  
              &lt;property name=&quot;hibernate.connection.username&quot; value=&quot;username&quot;/&gt;
              &lt;property name=&quot;hibernate.connection.password&quot; value=&quot;password&quot;/&gt;
  
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>