Skip to Content
author's profile photo Former Member
Former Member

NullPointerException:while trying to invoke the methodEntityManager.persist

Hi,

We are getting below error when we create an instance of the class which is invoking an entity manager to persist objects using JPA. It would be like a stateless bean creating an instance for the class which is invoking an entity manager to persist objects.

Error:

java.lang.RuntimeException: java.lang.NullPointerException: while trying to invoke the method javax.persistence.EntityManager.persist(java.lang.Object) of an object loaded from field com.persistence.service.manager.impl.CommonPersistenceInterfaceManagerImpl.em of an object loaded from local variable 'this'.

Please let us know are we able to create an instance for Persistable class to reuse it anywhere?.

Code Sample:

// Persistance Manager Class

public class CommonPersistenceInterfaceManagerImpl

{

@PersistenceContext(unitName="SAMPLE")

private EntityManager em;

public String save(Persistable pers)

{

em.persist(pers);

return "True";

}

}

// Stateless webservice

@Stateless

public class ManageImplBean implements Manage {

CommonPersistenceInterfaceManagerImpl cmp;

public ManageImplBean()

{

cmp = new CommonPersistenceInterfaceManagerImpl();

cmp.save(obj);

}

}

PS: If we have created an EntityManager instance in the same webservice then it is working fine.

Best Regards,

Karthika

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    Posted on Dec 15, 2011 at 11:22 PM

    Hi,

    in CommonPersistenceInterfaceManagerImpl the EntityManager em is not injected because CommonPersistenceInterfaceManagerImpl is no StateLess session bean but a pure java class that is instantiated in the other bean's class.

    For dependency injection of persistence context, CommonPersistenceInterfaceManagerImpl should itself be a SLSB and should not be instanciated in ManageImplBean but referenced via @EJB.

    Regards

    Rolf

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.