title: EJB @PersistenceContext EntityManager Throws NullPointerException
body: <p>I'm having a problem with injecting EntityManager by using @PersistenceContext. I try to inject EntityManager in EJB class with @PersistenceContext and I always get NullPointerException.</p>
  
  <p>Here is EJB class:</p>
  
  <pre><code>@Stateless
  public class BookEJB {
  
  public BookEJB(){
  
  }
  
  @PersistenceContext(unitName = "BookWebStorePU")
  private EntityManager em;
  
  public Book createBook(Book book) throws Exception {
      System.out.println("EM: " + em);
      em.persist(book);
      return book;
  }
  
  public Book findBookByIsbn10(String isbn10){
      Book book = new Book();
      em.find(Book.class, isbn10);
      return book;
  }
  //Other methods here
  }
  </code></pre>
  
  <p>Here's Persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
  &lt;persistence-unit name="BookWebStorePU" transaction-type="RESOURCE_LOCAL"&gt;
  &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
  &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
  &lt;validation-mode&gt;NONE&lt;/validation-mode&gt;
  &lt;properties&gt;
    &lt;property name="javax.persistence.schema-generation.database.action" value="create"/&gt;
    &lt;property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/&gt;
    &lt;property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/BookWebStoreDB"/&gt;
    &lt;property name="javax.persistence.jdbc.user" value="bookwebstoreadmin"/&gt;
    &lt;property name="javax.persistence.jdbc.password" value="password"/&gt;
    &lt;!-- Let EclipseLink create database schemas automatically --&gt;
    &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables"/&gt;
    &lt;property name="eclipselink.ddl-generation.output-mode" value="database"/&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>Here's my test file:</p>
  
  <pre><code>public class BookDaoTests {
  
  private BookEJB bookDao;
  private Book newBook;
  
  @Before
  public void init() {
  
      newBook = new Book();      
      newBook.setName("Flying Cow");
      newBook.setDescription("Super cool story about flying cow");
      newBook.setAuthor("Me");
      newBook.setIsbn10("0123456789");
      newBook.setIllustrations(true);
      newBook.setPublishYear(2013);
      newBook.setNumberOfPages(1567);
      newBook.setQuantity(58);
  
      bookDao = new BookEJB();
  
  }
  
  @Test
  public void createBook() throws Exception{
      bookDao.createBook(newBook);
      Assert.assertEquals("Book was created!", bookDao.findBookByIsbn10("0123456789"), newBook);
  }
  
  
  }
  </code></pre>
  
  <p>So when I run that test file I get following error:</p>
  
  <pre><code>Testcase: createBook(com.mysite.bookstore.tests.BookDaoTests): Caused an ERROR
  null
  java.lang.NullPointerException
  at com.mysite.bookwebstore.beans.BookEJB.createBook(BookEJB.java:27)
  at com.mysite.bookstore.tests.BookDaoTests.createBook(BookDaoTests.java:46)
  
  EM: null
  </code></pre>
  
  <p>I use following technologies:</p>
  
  <ul>
  <li>Glassfish 4</li>
  <li>JavaEE 7</li>
  <li>JSF</li>
  <li>EclipseLink 2.1</li>
  <li>Java DB</li>
  </ul>
  
  <p>I hope we can find some solution for this problem. I have been tackling now 3 days of this problem and searched and tested solutions from Google and from Stackoverflow but none of the solutions helped/worked. To make sure that the EntityManager was really null, I debugged test file and saw that indeed it gives null. So how can I solve this problem?</p>