Question

Using GlassFish 3.1.2, I try to call a Session Bean from a Netbeans platform module, and I get a null pointer exception. My problem is that I have no trace explaining how / where the NPE is generated.

The code in my Module is simply:

import ejb.MySessionRemote;
import javax.ejb.EJB;

public class TestServer {

    @EJB
    private static MySessionRemote mySession;

    public boolean execute() {
        System.out.println("result = " + mySession.getString()); //NPE here: mySession is null
        return true;
    }
}

The Session bean "My Session", the remote interface and the application deployed on the server side are just the ones from this tutorial: https://netbeans.org/kb/docs/javaee/entappclient.html

Any help greatly appreciated.

Note: I've checked this tutorial, without solving my issue.

Was it helpful?

Solution

If mySession is null, it was probably not injected. You can inject into managed beans (EJBs for example), because these instances are managed (created/removed) by a container, and the container does the injection for you.

You can possibly inject into a POJO, if you use CDI.

If TestServer is part of a stand-alone application for example, try to lookup the EJB using JNDI. This is what your tutorial does as well. It involves setting up the properties to get an InitialContext, and the lookup of the EJB using JNDI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top