Search code examples
javaeclipsejndiglassfish-4

NullPointerException in AppClient GF4


I am developing an application client that needs to access beans remotely on the gf server. From reading I thought I should be able to use injection annotations but this would not work for me. I then switched to using InitialContext looking up the mapped name of my bean but I can't get this to work either. I have looked at a few threads here on this and as far as I can make out all the jars I need should be included and I have added the EJB project to my appCLients path. This does not work from either in Eclipse or by clicking launch from the server admin pages.

My Bean in the EJB project

/**
 * Session Bean implementation class MazeBean
 */
@Stateless(mappedName = "mazes")
@Remote
public class MazeBean
{

The class I am trying to access from (not main which, I think, is why I couldn't use injection)

public class MazeBuilder
{
    private MazeBean mazeBean;  
    Maze maze;

    public MazeBuilder()
    {
        Context ctx;
        try
        {
            ctx = new InitialContext();
            mazeBean = (MazeBean)ctx.lookup("java:comp/env/mazes");
        } catch (NamingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        maze = mazeBean.getMaze(1); (line 31)
        mazeBean.regenerateMaze(maze);
    }

}

The exception I get when running in Eclipse

javax.naming.NamingException: Lookup failed for 'java:comp/env/mazes' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NameNotFoundException: No object bound for java:comp/env/mazes [Root exception is java.lang.NullPointerException]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at mazeBuilder.MazeBuilder.<init>(MazeBuilder.java:23)
    at Main.main(Main.java:16)
Caused by: javax.naming.NameNotFoundException: No object bound for java:comp/env/mazes [Root exception is java.lang.NullPointerException]
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:229)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:471)
    ... 4 more
Caused by: java.lang.NullPointerException
    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:159)
    ... 5 more
Exception in thread "main" java.lang.NullPointerException
    at mazeBuilder.MazeBuilder.<init>(MazeBuilder.java:31)
    at Main.main(Main.java:16)

Now it says that there is no object with that name so I am obviously not setting this up correctly. I was under the impression that the annotations are a legal method of definition so the mappedName attribute of my bean should allow me to find it. What else do I need to make the bean discoverable?

Eclipse Kepler, GF4, MySql 5.5.31, EclipseLink 2.5

I have tried both methods in the main also and get the same exception


Solution

  • I have got past this problem now, thanks Ajan, and it was adding gf-client.jar and appserv-rt.jar from the glassfish lib directory after I had corrected the global name.

    ps - How do I set a question as solved?