NullPointerException in JPA

前端 未结 2 1764
清酒与你
清酒与你 2020-12-21 21:57

I am creating a Java EE application using jpa.

I am getting the following error when the code is run on server

    [2/25/12 22:56:31:371 IST] 0000004         


        
相关标签:
2条回答
  • 2020-12-21 22:22

    in such cases make sure you write the@EJB annotation before creating the object of the class containing the entity manager. the reason for the null pointer exception is generally because the entitymanager is not initialised. @EJB annotation does that.

    In your case the class where you declare an object of user class write @EJB above it.this should solve the problem.

    0 讨论(0)
  • 2020-12-21 22:35

    Your EntityManager manager is no-interface an Enterprise Java Bean injected in your client UserBean. EJBs should be injected in classes whose lifecycle is managed by the application server.

    The client must be either a web component or another enterprise bean. In your case, the client UserBean is a POJO (Plain Old Java Object). See here for further details about how to use EJBs.

    A possible simple solution is to make UserBean a Stateless Session Bean by adding the annotation javax.ejb.Stateless before the class definition. And you should eventually inject UserBean into your ManagedBean with the EJB annotation.

    0 讨论(0)
提交回复
热议问题