1

I have to create project with interface(IGraphRemote), sessionbean(Graph) and client class(AppClient)

I tried to prepare simple project with one method register(int hwork, String album) - hwork its my homework's number, album its my album's number, but it doesn't work and returns an error: java.lang.NullPointerException , so connection wasn't established. How can i fix that ?

My files: IGraphRemote

public interface IGraphRemote {
    public boolean register(int hwork, String album);

}

Graph

@Remote
public class Graph implements IGraphRemote{
   public boolean register(int hwork, String album) {      
       return (hwork == 6
                && album.equals("119962"));
    }  
}

And AppClient

public class AppClient {

    @EJB
    private static IGraphRemote gremote;

    public static void main(String[] args) {

        AppClient ap = new AppClient();             
        System.out.println(ap.gremote.register(6, "119962"));
    }
}

here are my project project structure

Error:

Exception in thread "main" java.lang.NullPointerException
    at AppClient.main(AppClient.java:22)
Java Result: 1
Empi
  • 153
  • 2
  • 18
  • 1
    Can you post the full stacktrace instead of only `NullPointerException`? – xaviert Dec 06 '15 at 13:58
  • sure i added it in my post – Empi Dec 06 '15 at 13:59
  • Not an expert with EJB, so my advice might be useless, but did you have a look at http://stackoverflow.com/questions/25828542/how-to-create-a-remote-session-ejb-from-a-client and http://stackoverflow.com/questions/4168920/new-to-ejb-world-null-pointer-exception-in-ejb-client? They also reported a NPE similar like yours. – xaviert Dec 06 '15 at 14:02
  • Thx, for suggest. In fact they had simillar problem, but, their solution was based on various(different) packages, i need to create everything in one project in one package. – Empi Dec 06 '15 at 14:17

2 Answers2

1

It's obvious from your code and also in the stack trace that you try to run a standard j2se application with a main method from within a web application, so how do you expect dependcy injection to happen unless there is some other code create the ejb instance and inject it in the ejb reference. EJB is a J2EE component and it can't be created in standard java application, you need an ejb container with a naming service to manage the EJB lifecycle. You can create a standard j2se client by connecting to the naming service and lookup for the bean, but your EJB MUST be deployed in an ejb container in the first place such as TomEE, Glassfish, websphere,..etc

  • Yup, thats right, now im using EJB Application Client and EJB module to create Beans, and it works. Thx – Empi Dec 09 '15 at 17:08
0

First of all, your EJB bean can not be static, waht is desrcibed here: Injecting a static EJB, nonsense?

Second, if you want to inject your EJB bean, your app has to run in EJB container, like Glassfish or JBoss. If you run your app like simple Java SE applicaiton, your IGraphRemote gremote will always be null unless you initialize it by yourself.

Community
  • 1
  • 1
Szarpul
  • 1,531
  • 11
  • 21