cancel
Showing results for 
Search instead for 
Did you mean: 

NullPointerException:while trying to invoke the methodEntityManager.persist

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

rolf_paulsen
Active Participant
0 Kudos

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

Former Member
0 Kudos

Thanks Rolf. It is resolved.

BR

Karthika

Answers (0)