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

null retrieved from env-entry resource

Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
I have an env-entry defined in my web.xml and when I attempt to print it from a simple servlet all I get is null. And of course if I attempt to invoke any methods on the object I get a NullPointerException.

I read somewhere that this can happen if you're using version 2.4 instead of 2.5, so I changed to 2.5 and I still have the same issue. Here's the web.xml:



And here's the servlet. Displaying this env-entry is all it does:



Thanks for your help.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
The default package is invisible for all other packages. Classes in another packages cannot import and see classes in the default package. Java classes should be placed in packages whenever you want to expose them for public.

The same story goes on here. It would also only work under very specific circumstances. A specific Tomcat version, a specific JDK version and a specific server configuration. I wouldn't rely on this. Put Java classes in packages. Always.

Regarding to the NullPointerException, it will be more helpful if you tell the details. For example the stacktrace and the code line which caused it.
Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
Thanks for your response. I did throw it into a package, thanks for the reprimand. Laziness due to this being just a test project, but you're right.

Anyway, I changed the servlet so it would throw a NullPointerException instead of just printing null. Here's the new servlet code:



And here's the stack trace. It's pretty much what you'd expect.

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458

You forgot to point out line 21.

Anyway, let's assume that line 21 is the following:

There are three calls on a reference.

response.getWriter(), writer.print() and welcomeMessage.toString()

One of them have thrown a NullPointerException. Do you understand when it would happen? It would happen when the reference is null! Thus either response, writer or welcomeMessage are null. According to the API documentation, response and writer should never be null. That would have been a bug. So the only left over is welcomeMessage. Looking at your code, you aren't instantiating it anywhere. It is indeed null.

To fix your problem simply instantiate it with a value, or leave the toString() call away as it is in fact superfluous.

Too bad by the way that you changed the code in the initial topicstart too much without testing it. No one would have spotted the NullPointerException soon.
Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
I understand that welcomeMessage is null and that I haven't instantiated it. As I understand it, I shouldn't have to instantiate it because the @Resource annotation is supposed to locate the env-entry block in web.xml and inject the env-value into welcomeMessage. There are examples of this on Sun's site: http://java.sun.com/developer/technicalArticles/J2EE/injection/index.html under the heading "A Simple Servlet". I believe my code matches the example.

However this is not working as I'd expect it too. The idea is that I don't have to instantiate the object and it will be instantiated for me. This way I can use environment-specific properties (support email addresses, WSDLs, etc) without headache. I hope this clears up the situation.

Also, I know that the toString() is superfluous; I added it so that I'd get the stack trace you asked for.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Oh, I see. Sorry, I missed that point from the topicstart.

I read somewhere that this can happen if you're using version 2.4 instead of 2.5, so I changed to 2.5 and I still have the same issue. Here's the web.xml
Are you sure that the web.xml is deployed correctly and the appserver is restarted correctly? Did you also check the appserver's startup logs if you don't see something weird?
Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
The server seems to restart normally and I didn't notice any warning or error messages in the log. But you're right to wonder about that; I have occasionally had problems with applications not recognizing an update to the web.xml file but recognizing other file updates. Hmm...
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Cameron ,

If you arent sure of whether web.xml is not picked up after server restart , try looking env-entry value using JNDI lookup as ctx.lookup(java:comp/env/welcomeMessage).
Cameron Dalton
Greenhorn

Joined: Nov 14, 2008
Posts: 19
Balu, that's a great idea. I did that and was able to lookup the value, which means the web.xml change was picked up. I'm not sure what's going on here. But I'm plugging away and will hopefully make some progress soon. Any more ideas are certainly welcome!

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: null retrieved from env-entry resource