File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Got NullPointer exception when injecting EntityManger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Got NullPointer exception when injecting EntityManger" Watch "Got NullPointer exception when injecting EntityManger" New topic
Author

Got NullPointer exception when injecting EntityManger

John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
I'm using JBoss 4.2.2. In my DAO class, I have:

....
@PersistenceContext(unitName="my_unit")
EntityManager entityManager;


....

Somehow, JBoss did not inject entity manager in my DAO so I got
a nullpointer exception when I access the entity manager.

Here is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="my_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/OracleTestDS</non-jta-data-source>
</persistence-unit>
</persistence>

my datasource file (my-ds.xml):

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>OracleTestDS</jndi-name>
<connection-url>
jdbc racle:thin:@localhost:1521:XE
</connection-url>
<!-- The driver class -->
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>uid</user-name>
<password>pw</password>
<min-pool-size>1</min-pool-size>
<max-pool-size>3</max-pool-size>

<idle-timeout-minutes>15</idle-timeout-minutes>

<new-connection-sql>
SELECT 'new connection' FROM dual
</new-connection-sql>

<check-valid-connection-sql>
SELECT 'connection from pool' FROM dual
</check-valid-connection-sql>

<track-statements/>
<metadata>
<type-mapping>Oracle10g</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
110

Originally posted by John King:
Somehow, JBoss did not inject entity manager in my DAO so I got
a nullpointer exception when I access the entity manager.



The EntityManager will be injected only in EJBs.


[My Blog] [JavaRanch Journal]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

Originally posted by Jaikiran Pai:


The EntityManager will be injected only in EJBs.


Not always. I've used OpenJPA in JBoss. However, since OpenJPA is not part of the container, I used Spring to instantiate the EntityManager. The injection of the EntityManager is done by the weaver, if I'm not too confused here.

The main thing is that the app has to have some specific sort of persistency framework mechanism defined for it - not just the rules that the framework will be using.


Customer surveys are for companies who didn't pay proper attention to begin with.
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
Originally posted by Jaikiran Pai:


The EntityManager will be injected only in EJBs.


The DAO class is called by a stateless EJB so it is part of the EJB. I just
refactored it out into a separate class so some code can be reused.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
110

Originally posted by Tim Holloway:

Not always. I've used OpenJPA in JBoss. However, since OpenJPA is not part of the container, I used Spring to instantiate the EntityManager. The injection of the EntityManager is done by the weaver, if I'm not too confused here.



Valid point. The application server itself is not going to inject the EntityManager, in components other than EJB. Frameworks like Spring (which i don't have an experience on) are capable enough to inject these dependencies.

Originally posted by John King :
The DAO class is called by a stateless EJB so it is part of the EJB.


John, the application server is going to parse the EJB class to scan for any resources which have to be injected. However, its not going to scan the classes which are used in the EJB. The DAO is just being used by the EJB and is not an EJB in itself.
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
Originally posted by Jaikiran Pai:


John, the application server is going to parse the EJB class to scan for any resources which have to be injected. However, its not going to scan the classes which are used in the EJB. The DAO is just being used by the EJB and is not an EJB in itself.


I actually changed the code so the EntityManager is defined in the EJB:

@Stateless
public class MyServiceBean implements MyServiceRemote
{
@PersistenceContext(unitName="my_unit")
EntityManager entityManager;

//business methods...
...
}

When I tried to access the entityManager in business methods, it is still null.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9327
    
110

Originally posted by John King:


I actually changed the code so the EntityManager is defined in the EJB:

@Stateless
public class MyServiceBean implements MyServiceRemote
{
@PersistenceContext(unitName="my_unit")
EntityManager entityManager;

//business methods...
...
}

When I tried to access the entityManager in business methods, it is still null.


That's strange. On the same 4.2.2 version of JBoss, i have several such examples where the entitymanager is injected in the EJBs. Do you see any exceptions during deployment? And can you post the logs when your application is being deployed? Also, does changing the injection to



work?
John King
Ranch Hand

Joined: Aug 27, 2002
Posts: 165
My bad. It was because of a typo.
It works now. Thank Jaikiran.
Douglas Boff Nandi
Ranch Hand

Joined: Feb 25, 2008
Posts: 34
Hi

How do you solve this problem? I have the same problem now.

Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Got NullPointer exception when injecting EntityManger
 
Similar Threads
Using EntityManager from a servlet?
Got NullPointer exception when injecting EntityManger
Invalid character ':' in value part of property (JBoss-ds.xml)
javax.persistence. TransactionRequiredException
Deployment unsuccessful: " service=DataSourceBinding State: NOTYETINSTALLED"