File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JBoss/WildFly and the fly likes @Resource annotation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "@Resource annotation question" Watch "@Resource annotation question" New topic
Author

@Resource annotation question

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hi,

I am using @Resource annotation for injecting the data source as below:

But, I get a Null Pointer Exception.

If i do a JNDI look up like this it works



What am i doing wrong with injecting the data source?

Thanks,
Srikanth.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10264
    
168

Which version of JBoss and Java do you use? And where are you injecting this resource?


[My Blog] [JavaRanch Journal]
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
Which version of JBoss and Java do you use? And where are you injecting this resource?


I am using JBoss 4.2.3GA with java 6. I am trying to inject the resource in a servlet at class level.
[ December 04, 2008: Message edited by: Srikanth Reddy ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10264
    
168

What does your web.xml look like? Have you used the web-app 2.5 xsd in the web.xml?
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
What does your web.xml look like? Have you used the web-app 2.5 xsd in the web.xml?


Is it mandatory to use web-app 2.5 xsd? Also do i need to add <resource-ref> element in the web.xml?
[ December 04, 2008: Message edited by: Srikanth Reddy ]
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
What does your web.xml look like? Have you used the web-app 2.5 xsd in the web.xml?


Jaikiran,

Here is how my web.xml looks like:


I am trying to inject the resource in servlet as:


Do i need to add resource-ref element in web.xml?

Thanks,
Srikanth.
[ December 04, 2008: Message edited by: Srikanth Reddy ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10264
    
168

No you don't need a resource-ref in the web.xml
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10264
    
168

Try this:



Assuming that you are deploying a -ds.xml file to bind the OracleDS.
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Originally posted by Jaikiran Pai:
Try this:



Assuming that you are deploying a -ds.xml file to bind the OracleDS.


Jaikiran,

I already did what you said with no luck. I spent whole day on this issue but didn't get any solutions.

Today i downloaded JBoss 5.0.0GA amazingly the same code worked!

I thought @resource annotations work in JBoss 4.2.3 but now i think they only work with JBoss 5.x versions. Is that correct?

Thanks,
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10264
    
168

Originally posted by Srikanth Reddy:
Jaikiran,

I already did what you said with no luck. I spent whole day on this issue but didn't get any solutions.

Today i downloaded JBoss 5.0.0GA amazingly the same code worked!

I thought @resource annotations work in JBoss 4.2.3 but now i think they only work with JBoss 5.x versions. Is that correct?



I did know that @EJB injection would not work in JBoss-4.2.x, but i thought the @Resource would work. Infact, i think i even had an example on my previous machine with JBoss-4.2.3 where it had worked (if i remember correct). Once i get my new machine setup properly, let me give this a try.

Good to know, it worked with JBoss-5 GA which got released today.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: @Resource annotation question