Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

java - New to EJB world... Null pointer exception in EJB client

I am learning EJB and I am trying to execute the Helloworld example given in EJB In Action book.

My app server is JBoss, I created the Jar file for the bean class and interface in the right directory( I can see the EJB in JMX console).

Now I created a simple client using EJB annotations, but I am getting a NullPointerException.

Here is my client code.

Client code:

package com.client;
import javax.ejb.EJB;
import com.EJB.*;

public class HelloWorldClient {
 @EJB
 private static HelloWorldInterface HelloBean;

 public static void main(String[] args)
 {
  HelloBean.SayHelloWorldInEJB();
 }
}

Bean class

package com.EJB;

import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorldInterface {

 public void SayHelloWorldInEJB() {
  // TODO Auto-generated method stub
  System.out.println("Hello world from the world of EJB");

 }
}

Interface

package com.EJB;
import javax.ejb.Local;;

@Local
public interface HelloWorldInterface {
 public void SayHelloWorldInEJB();
}

Note: I tried using specifying the interface as Remote, it still didn't work.

Steps that I did so far to get to this point. 1) Created the file EJB files 2) Made the build.xml and deployed the EJB.

Am I missing any configuration files ???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Now I created a simple client using EJB annotations, but I am getting a NullPointerException.

Your client code looks like an Application Client and such client is supposed to be deployed on the app server and then executed in an Application Client Container (ACC) so that injection can occur. Starting the ACC requires an application server specific command.

The following wiki explains the usage of the ACC in JBoss (how to package, deploy and launch an ACC): How to use an application client in JBoss-5.

If you don't want to use an Application Client Container and instead just run the application client class through a java command, injection won't be possible and you'll have to perform a JNDI lookup.

And in both cases, you'll have to provide and use a remote business interface for your bean.

Resources

Related questions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...