Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Need help with @EJB dependency injection null pointer

meatwadMar 2 2011 — edited Mar 4 2011
I have an application in an ear file that contains 2 modules. One is a web component war and the other is an ejb jar.
appname.ear
+-webclient.war
+-webbean.jar
I have declared my bean with a @Stateless annotation:
@Stateless
public class EchoBean {}
And added a dependency with @EJB in a servlet in my web module:
@EJB(lookup="java:app/webbean/EchoBean")
private EchoBean _echo;
Everything compiles but when I run, this object always comes up null. Using a JNDI lookup on the InitialContext I can successfully get the bean via "java:app/webbean/EchoBean". But using that as the lookup attribute in the @EJB also fails. What could I be doing wrong?
This post has been answered by gimbal2 on Mar 4 2011
Jump to Answer

Comments

jtahlborn
what happened when you tried the EJB annotation without any parameters?
meatwad
null pointer exception. That was why I started experimenting with different options.
gimbal2
is this on a JEE6 or JEE5 container?

If JEE5, you need at least a local interface. Although the JNDI lookup should have also failed then... I'm stumped. Just for the sake of ruling out possibilities, what if you do add a local interface to the EJB? Does the problem go away then? Do remove the annotation parameter, you shouldn't need it.
meatwad
I'm running in Glassfish 3.0.1 Java EE 6. I added a local interface but I get the same null pointer exception. By the way I am new to beans.
gimbal2
Okay. So no injections are happening in the servlet. Do injections in other resources work?

For the sake of completeness, post your web.xml please.
meatwad
Here's my application.xml for my ear file:
<?xml version="1.0" encoding="UTF-8"?>

<application
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/application_6.xsd"
  version="6">

  <application-name>testapp</application-name>

  <module>
    <web>
      <web-uri>webclient.war</web-uri>
      <context-root>client</context-root>
    </web>
  </module>

  <module>
    <ejb>webbean.jar</ejb>
  </module>

</application>
Here's the web.xml for my webclient.war:
<?xml version="1.0" encoding="UTF-8"?>

<web-app
  version="2.4"
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <servlet>
    <servlet-name>Inform</servlet-name>
    <servlet-class>test.InformServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>Inform</servlet-name>
    <url-pattern>/inform</url-pattern>
  </servlet-mapping>

</web-app>
I'm not using using an ejb-jar.xml file in the ebj jar. That's optional right?
meatwad
Also to answer your question I made a separate test web module and placed a test bean inside of it using annotations in a test servlet and bean. I deployed the module to glassfish and it worked perfectly.
gimbal2
Answer
Meatwad wrote:
<?xml version="1.0" encoding="UTF-8"?>

<web-app
version="2.4"
2.4? I'm venturing a guess that this right here is your problem; set that to 2.5 and try again.
Marked as Answer by meatwad · Sep 27 2020
meatwad
That was the problem. Thanks a bunch. I updated to the 3.0 schema: http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Apr 1 2011
Added on Mar 2 2011
9 comments
1,563 views