• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question with @EJB annotation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Edwin for the prompt reply.

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

Thank you again
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Venus Ong
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Whatever you say buddy! And I believe this tiny ad too:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic