• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error while trying to run StatelessSessionBean

 
Rakesh Rajagopalan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Im very new to EJB 3. I was trying to run a very simple SessionBean, and Im getting the NullPointerException. The code are as follows:

BeanClass(serverpack.BeanClass)
--------------------------------
package serverpack;

import javax.ejb.Stateless;

@Stateless
public class BeanClass implements BeanInterface {
public void message() {
System.out.println("Hello World");
}
}
Component Interface(serverpack.BeanInterface)
----------------------------------------------
package serverpack;

import javax.ejb.Remote;

@Remote
public interface BeanInterface {
void message();
}
I compiled these two classes successfully, added them to myapp.ear and deployed them in the GlassFish server.

I wrote a client class, which is as follows:
Client Class: (clientpack.BeanClient)
-------------------------------------
package clientpack;

import javax.ejb.EJB;
import serverpack.*;

public class BeanClient {
@EJB
private static BeanInterface bInterface;

public static void main(String... args) {
bInterface.message();
}
}

When I tried to run this from the command line, I got a NullPointerException. Im sure Im missing some step. Could anyone of you please guide me through this?

Kind Regards,
Rakesh.R
SCJP 6, SCWCD 5.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,

What you are doing is as very common mistake. In a command-line application, there is nothing to handle @EJB unless you are using the Application Client Container (ACC). For a beginner, I would suggest using JNDI look-up instead in a command-line app or just use @EJB in a Servlet. Running the ACC will require at least intermediate level understanding of Java EE.

Regards,
Reza
 
Rakesh Rajagopalan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives you a wonderful feeling to know that you are interacting with the author if the book that you are using! Im currently using EJB 3 In Action and it is a privilege to interact with Mr.Reza Rahman. Thank you so much for the reply!
 
Rakesh Rajagopalan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clue Reza. I got what you said.
 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rakesh,

It's my privilege. Sorry I can't always be available and be very detailed. Do let me know if you need more help.

Cheers,
Reza
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!