• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Got NullPointer exception when injecting EntityManger

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
John King
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. It was because of a typo.
It works now. Thank Jaikiran.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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

Thank you
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic