File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes EJB3 - NullPointerException with @EJB Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3 - NullPointerException with @EJB Injection" Watch "EJB3 - NullPointerException with @EJB Injection" New topic
Author

EJB3 - NullPointerException with @EJB Injection

Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Hi, Ranchers

After getting very disappointed at EJB 2.1, I am migrating back to EJB 3, it seems pretty good. I have everything set up: persistence, the client (web) tier and so on. In a test application I have a private member variable annotated with @EJB (its type is the remote business interface of a stateless session). The referred session shows up in JBoss JNDI correctly. Now, when I try to access the injected variable it is null, meaning it has NOT been injected by the container. Then I recoded it to make a JNDI lookup and it worked out. My question is: did I fail somewhere, should I really not use dependency injection or is it just not working?

Just for the record: I downloaded the most recent JBoss version as of last week.

Kind regards,
Henrique


Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
Andreas Schaefer
Ranch Hand

Joined: Feb 13, 2006
Posts: 63
It works from my experience so please provide code, JBoss info etc. for us to make an assesment.
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
I coded everything in my native language, Portuguese, so try to abstract the names ok? Well, here are some snippets:
First, the business interface (I made it Remote):
Then the implementation:
And the web component (a JSF action) that uses it:
These classes are in separate archives. The web component is in the WEB-INF/classes directory while the session is in the ejb jar. Both are package together in the same .ear file, and both are referred to in application.xml. No compilation or runtime exception, except for the NullPointerException of course.
Oh! And I added nothing concerning EJB to jboss-web.xml, web.xml, jboss.xml and ejb-jar.xml.
[ March 28, 2006: Message edited by: Henrique Sousa ]
Andreas Schaefer
Ranch Hand

Joined: Feb 13, 2006
Posts: 63
AFAIK you can ONLY use dependency injection in EJB3 right now. If I am not mistaken JBoss 4 with EJB 3 does not support Java 1.5 in the Catalina code generator (generics do not seem to work) and so I think that EJB3 also is not supported as well.

I guess you have to use the good old JNDI lookups.

-Andy
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
that's odd... I guess you are talking about JSP compilation, aren't you? Because my first attempt to inject the EJB was with a ThreadLocal, something like this (I don't have the original code here):

This was just a safety measure. As you can see, it contains both annotations and generics, but I got no error on both; just the old NullPointerException. As for the JSP generation... well, I don't intend to write Java code directly in them, so they can take as long as they wish as far as I'm concerned.
Andreas Schaefer
Ranch Hand

Joined: Feb 13, 2006
Posts: 63
The dependency injection is done by the container. JBoss provides that for EJBs but I don't think for web components. I am not an expert on JSF and so I do not where that code is executed.

-Andy
Stephen ConnollyApril
Greenhorn

Joined: Apr 06, 2006
Posts: 1
Check that the xml files are all using the correct version.

I had a similar problem with injecting into a servlet. It was fixed by updating the web.xml to use the newest version of the schema. Alternatively you can delete the .xml descriptors and a JavaEE5 compliant container should be able to figure everything out... although some tools will fall over when you have a .war with no web.xml (NetBeans are you listening?)
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Okay, so I checked the project end to end. Both .war and EJB .jar are in the same .ear file; application.xml declares them. Here are my configuration files versions:

I also checked that they are all in their right places (META-INF | WEB-INF) The injection is still not happening.
Can I inject an EJB in any class or only in servlets? Because I'm injecting into a JSF action (a POJO).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3 - NullPointerException with @EJB Injection
 
Similar Threads
EJBContext injection and ACC
Why not use Dependency Injection of Stateful session bean inside a stateless session bean?
Polymorphism & DI
Dependency Injection wiht beanName property
@EJB