jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes Question with @EJB annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Question with @EJB annotation" Watch "Question with @EJB annotation" New topic

Question with @EJB annotation

Venus Ong

Joined: Dec 10, 2009
Posts: 7
Hello everyone

I am using EJB 3.0, and weblogic 10.3.

I have created a Stateless session bean, and it implements two interfaces - StatelessLocal and StatelessRemote. I have annotated them with the @Stateless, @Local, @Remote for the above classes.

And then I tried to create a client class with this initialization:

Later in the main method:

When my compiler hits the line above, it gives me a NullPointerException.

Can you tell me where I've gone wrong?

And I have another question about declaring the @EJB part, do we always need to use static to declare the session bean?

Any response is greatly appreciated. Thanks!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Actually, you should use the @EJB annotation on instance members not on static ones. If you think it over, this totally makes sense, because the EJB injected should not be shared by multiple threads, something you cannot guarantee if you inject it into a static variable.

On the other hand, the injection can only happen on container-managed classes, like a Servlet, another Enterprise Java Bean component, even JSP or JSF, but you cannot make the automatic injection happen in helper classes. For those cases, you will need to use JNDI as in the old days.

I hope this helps!
Venus Ong

Joined: Dec 10, 2009
Posts: 7
Thank you Edwin for the prompt reply.

Can you please elaborate further on "use JNDI as in the old days"?

Thank you again
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

Edwin Dalorzo wrote:Actually, you should use the @EJB annotation on instance members not on static ones.

Actually, looking at what Venus seems to be doing, it appears to be an "application client" (with a main method). Injection in application client's requires the member being injected to be static.

And furthermore, each application server has its own way to running an application client (in an application client container). Venus, which application server and which version of the server do you use? The application server documentation will give you more details on how to run the application client.

[My Blog] [JavaRanch Journal]
Venus Ong

Joined: Dec 10, 2009
Posts: 7
Thanks for your reply, Jaikiran.

I am currently using Web Logic 10.3.1 (Oracle), and my IDE is JDeveloper.

Do you know what should I do?

And I will check out the application server's documentation on my problem too
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
Are you sure that you deployed the SessionBeans? And secondly, there is a serious error in you code

You wrote

you should inject the interface

JNDI sees the business interface, not the class itself. Depending on whether you will use methods from the local or remote interface, you should inject the correct interface - then the right session bean will be injected

I agree. Here's the link: http://aspose.com/file-tools
subject: Question with @EJB annotation
It's not a secret anymore!