Get your CodeRanch badge!*
The moose likes EJB and other Java EE Technologies and the fly likes NullPointer Exception in EJB 3.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "NullPointer Exception in EJB 3.0" Watch "NullPointer Exception in EJB 3.0" New topic
Author

NullPointer Exception in EJB 3.0

Sagun Khatri
Greenhorn

Joined: Dec 21, 2007
Posts: 3
I am sorry if someone has already posted this question before. If someone please point me towards the right direction I would be really grateful.

I am writing a very simple EJB 3.0 Session Bean (Stateless) using Netbeans 6 and GlassFish server.

I am trying to invoke the Remote Interface from an Application Client, but I get a NullPointerException. The problem is my Client Application can't find the Remote Interface. Here is my code

*************** Remote Interface *****************
package ejb30;

import javax.ejb.Remote;

@Remote
public interface StateLessRemote {
public String hello();
}


************** Stateless Session Bean *************
package ejb30;

import javax.ejb.Stateless;

@Stateless
public class StateLessBean implements StateLessRemote {
public StateLessBean() {}

public String hello()
{
return "*** Hello World! ***";
}
}


*************** Aplication Client ******************
package stateLessClient;

import javax.ejb.EJB;
import ejb30.StateLessRemote;

public class StateLessAppClient
{
@EJB(mappedName="ejb03.StateLessRemote")
private static StateLessRemote stateLessBean;

public static void main(String[] args)
{
System.out.println("*** Calling Stateless bean ****");
System.out.println(stateLessBean.hello());
}
}


********** Error from Netbeans *******************
Dec 21, 2007 3:15:12 PM com.sun.enterprise.appclient.MainWithModuleSupport <init>
WARNING: ACC003: Application threw an exception.
java.lang.NullPointerException
at stateLessClient.StateLessAppClient.main(StateLessAppClient.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Sagun Khatri
Greenhorn

Joined: Dec 21, 2007
Posts: 3
The solution to the above NullPointer Exception in EJB 3.0 is as follows:

1) Create an Enterprise Application Project in NetBeans 6.0

2) Create new Session Bean, write your session bean code in here.

3) Create a Remote Interface, include the method(s) from the above Session Bean.

4) Create a Application Client in Secure-apple-client node.

5) Write an annotation to call the EJB in the application client.

6) In Java EE 5 we are not required to write a deployment descriptor (ejb-jar.xml). However, if we do have security for the application, we need to specify that in the deployment descriptor.
Sagun Khatri
Greenhorn

Joined: Dec 21, 2007
Posts: 3
Hello again, please follow this link to the NetBeans 6.0 tutorial on how to create a Secure Enterprise Bean in Java EE 5.

http://www.netbeans.org/kb/60/javaee/secure-ejb.html
[ December 24, 2007: Message edited by: Sagun Khatri ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NullPointer Exception in EJB 3.0
 
Similar Threads
Java SE6 client for EJB - Error in lookup
Connection to remote Glassfish server
deployment descriptor (ejb-jar.xml) problem
Look up EJB 3.0 Session Bean in Remote Client
using EJB annotation