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]
# 1
I am not a STRUTS expert, but could you remove the static from @EJBstatic FleetoolSessionLocal sess;into@EJBFleetoolSessionLocal sess; Anyway, I'll forward this question to another team member
Mahesh.Kannana at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thanks for the reply.I removed the static and the problem remains.Sam
sammaesa at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Looks like the ActionClass is not a managed class and hence the container will not inject anything into it. I have forwarded this to the web container team and they will confirm this soon.
Mahesh.Kannana at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Mahesh is right: Struts action classes are not considered container-managed components from a Java EE point of view, and therefore will not be injected by the servlet container. See the Servlet 2.5 specification for a list of container-managed classes.
jan.luehe@sun.coma at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Here's an EJB FAQ entry that explains how to access dependencies such as local ejb referencesfrom non-managed classes :https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB --ken
ksaksa at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Ken,Is it good practise to have a POJO where it invokes session beans but this POJO will have to know that its caller needs to be a session bean or a servlet, otherwise it will break? ThanksJune
June_Ma at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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

ksaksa at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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.

sammaesa at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 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

ksaksa at 2007-7-12 9:43:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...