0

I have the following error using JPA with Eclipse : EntityManager : NULLpointerException.

The code of the class is :

@ManagedBean(name="inputHStockManager")
@RequestScoped
public class InputHStockManager
{   
  @PersistenceContext(unitName="inventory")
  EntityManager em ;

  public void getHStockData(){
      System.err.println("\n\n" + em + "\n\n");

      List<InputHStock> list = em.createQuery("FROM InputHStock").getResultList();
  }
}

If I declare the EntityManagerFactory directly in the class :

  EntityManagerFactory emf =  Persistence.createEntityManagerFactory("inventory"); 

Then it is working, so the error is related to the injection but whatever I try I don't succed in solving it.

Soji
  • 177
  • 1
  • 5
  • 17
  • It may depend on which version of Java you are using. Before Java version 7, you may not be able to inject the EntityManager directly into a managed bean. You may have to instead inject the EntityManager into an EJB, and then inject the EJB into your managed bean. – dave823 Aug 21 '15 at 18:16
  • I am using Java7. I also tried using an EJB but have the same problem with the nullPointerException . – Soji Aug 21 '15 at 18:20
  • Can you post your persistence.xml? – whitlaaa Aug 21 '15 at 20:25
  • Are you sure you are not using constructor to create objects of this class? The code sample posted in the question seems OK - EntityManager can be injected into many types of managed beans, including CDI and JSF beans. Null pointer usually indicates that the bean is not created by the container and therefore it is not managed. – OndroMih Aug 22 '15 at 22:02

0 Answers0