Reputation: 321
I am creating a new EJB project with beans that are used in another simple project. I am using NetBeans 7.4 with Glassfish 4.0. Here are my Interface and implementation for the EJB project :
DbBeanInt.java
package com.ejb;
import javax.ejb.Remote;
@Remote
public interface DbBeanInt {
public void test(String asd);
}
DbBean.java
package com.ejb;
import javax.ejb.*;
@Stateless(name = "DbBean", mappedName="B")
@Remote
public class DbBean implements DbBeanInt{
@Override
public void test(String asd) {
System.out.println(asd);
}
}
And here is the code where I am calling it. I included in this project's library the EJB project.
package bookstoreclient;
import com.ejb.DbBeanInt;
import javax.ejb.EJB;
public class BookStoreClient {
@EJB
private static DbBeanInt db;
public static void main(String[] args) {
db.test("Test");
}
}
However when I run this application I get :
Exception in thread "main" java.lang.NullPointerException
at bookstoreclient.BookStoreClient.main(BookStoreClient.java:12)
Is there something else that should be included?
Upvotes: 0
Views: 979
Reputation: 1465
The DbBean class and DbBeanInt interfaces looks ok. but the BookStoreClient class is not correct.
you are using @EJB to "inject" a EJB Proxy to communicate to your remote EJB. You can only inject "Managed Beans" (and a EJB is a Managed Bean managed by EJB Container) into "Managed Beans". And your BookStoreClient class is not a managed bean because you start it with the main() method.
i have made a Repository on GitHub to show you an working Example for your question: https://github.com/StefanHeimberg/stackoverflow-27411885
simply clone it and open the _27411885 with Netbeans.
inside this repository you will find the db-ejb project with the DBService Bean and the DBServiceRemote interface. there is also a DBServiceIT with uses an embedded glassfish to test the DBService over the local no-interface view (@LocalBean).
the db-client project simply calls the DBService bean remotely. be sure to first deploy ("Run") the db-ejb project with glassfish.
Upvotes: 1