Search code examples
javanetbeansejbejb-3.0

Dependency Injection in one project


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

Solution

  • 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