EJB3 - NullPointerException with @EJB Injection
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It works from my experience so please provide code, JBoss info etc. for us to make an assesment.
Henrique Sousa
Ranch Hand
Posts: 92
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
Andreas Schaefer
Ranch Hand
Posts: 63
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
I guess you have to use the good old JNDI lookups.
-Andy
Henrique Sousa
Ranch Hand
Posts: 92
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
Andreas Schaefer
Ranch Hand
Posts: 63
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-Andy
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?)
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
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 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).
Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
| What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









