This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes @EJB giving NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "@EJB giving NullPointerException " Watch "@EJB giving NullPointerException " New topic
Author

@EJB giving NullPointerException

Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
Hi,

I have a sample EJB3 project that has one Stateless session bean and an Application Client (main method) is calling its
method using JNDI lookup and its working fine.

But when I change the code and instead of JNDI lookup I inject EJB3 using @EJB annotation
its not able to find the bean and giving NullPointerException .

Just wondering is there any other setting requires to inject an EJB3 component.

Thanks.
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Mishaal, you cannnot access @EJB in main method. @EJB will be injected by the server to your program and that program should be managed by the server itself(Servlets or EJBs).

So, in your main method you cannot access @EJB as its not available. So for main method you need to use only JNDI lookup.

-Sunil
Mishaal Khan
Ranch Hand

Joined: Aug 21, 2008
Posts: 61
Thanks for your reply Sunil.

I also read the same description in a book that EJB can be injected in registered component in server.But
I am confused with this tutorial which is injecting EJB in a client program.

http://java.sun.com/javaee/5/docs/tutorial/doc/bnbnj.html


Do you think this application client can be registered in server?
Please verify how come it is possible.

Thanks.
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
I have seen the link provided by you. At this point, i am not very much sure now. So let me try above example and then see....

Thanks for above link

Sunil
xsunil kumar
Ranch Hand

Joined: Dec 14, 2009
Posts: 133
Mishaal, i have tried the link given above and i am getting Null pointer exception there. I am not sure how sun has given this example.
 
wood burning stoves
 
subject: @EJB giving NullPointerException
 
Similar Threads
@EJB doing nothing - jboss-4.2.2.GA
Is there any point to creating some kind of cache for lookups in EJB3?
@EJBs annotation.
@EJB annotation is not working in weblogic 10.3
How to obtain entity manager in application server?