nullpointerexception when accessing local EJB3.0 method
Hi all,
I'm trying to replace some of the business logic in an existing struts application by EJB3.0. I first made the simplest stateful sessionbean and try to call it via a local interface. When doing this I always get a nullpointerexception when the "sess.hello()" is called.
What am I doing wrong here ? I use netbeans5.5, struts1.2.9.
The run JavaEE version of my struts project is 1.4 and I can't change it, could this be the problem as annotations to EJB were only introduced in 1.5, but this should give compile errors shouldn't it ?
the code of my ejb:
@Stateful
public class FleetoolSessionBean implements fleetool.FleetoolSessionLocal {
private String greeting = "Default hello !";
/** Creates a new instance of FleetoolSessionBean */
public FleetoolSessionBean() {
}
public String hello()
{
return greeting;
}
}
the code in my struts action class:
@EJB
static FleetoolSessionLocal sess;
sess.hello();
Thanks in advance,
Sam
[1071 byte] By [
sammaesa] at [2007-11-27 4:33:39]

# 7
Implementing the POJO such that it is agnostic to its caller component type does require some extra
care when it comes to accessing environment dependencies since by definition those dependencies
are component-scoped.It's less of an issue to share a POJO within a web application since in the
case of .war there is one component environment for the entire web application.
If you do need to use the same POJO class across invocations from a mixture of web components and
EJBs, and that POJO needs to access environment dependencies, your best bet is to just define the
dependency with the same name for each of the calling component environments. That way the
POJO can just do a java:comp/env lookup without caring who it's being invoked from.
--ken
# 8
Ok, I wanted to take advantage of annotations in EJB3.0 but I had to start messing in the XML descriptors after all ...
After a lot of experimenting I managed to make descriptors that actually deploy, but I still can't acces my EJB.
In sun-web.xml I have
<ejb-ref>
<ejb-ref-name>FleetoolSessionBean</ejb-ref-name>
<jndi-name>ejb/FleetoolSessionBean</jndi-name>
</ejb-ref>
and in web.xml
<ejb-ref>
<ejb-ref-name>FleetoolSessionBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>fleetool.FleetoolSessionLocal</home>
<remote>fleetool.FleetoolSessionRemote</remote>
</ejb-ref>
In the action class:
InitialContext ic = new InitialContext();
FleetoolSessionLocal ftlocal = (FleetoolSessionLocal)ic.lookup("java:comp/env/FleetoolSessionBean");
This code gives:
javax.naming.NameNotFoundException: FleetoolSessionBean not found
Can someone point me in the right direction please ?
Thanks in advance.
# 9
Are you sure the <home> element in your ejb-ref is correct?It seems odd that it would be
called FleetoolSessionLocal is it's the Home interface of a Remote EJB.Other than that,
the structure of the xml entries looks fine and the java:comp/env lookup correctly matches
the ejb-ref-name.Another thing you can do to check the app is run the verifier tool in the
Java EE SDK bin directory. It can find lots of errors in the code and deployment descriptors.
--ken