• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

EJB3 - NullPointerException with @EJB Injection

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works from my experience so please provide code, JBoss info etc. for us to make an assesment.
 
Henrique Sousa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic