File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes InitialContext.lookup always returns NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "InitialContext.lookup always returns NullPointerException" Watch "InitialContext.lookup always returns NullPointerException" New topic
Author

InitialContext.lookup always returns NullPointerException

Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
I've migrated an application from Tomcat 4.0 to Eclipse 3.0 using Tomcat 5.0. Since I ran into some problems accessing a database once I moved to Eclipse and a new PC I decided to experiment with DataSource and JNDI. I mention all this just because there are so many new factors here: new PC, Tomcat 5.0, Eclipse, DataSource and JNDI and I suppose any of them could be the cause of my problem.

But I think that the problem is in either DataSource or JNDI. In my code I create an InitialContext and then use that to get a Datasource.


Eclipse compiles the entire web application without complaint but the lookup always returns a NullPointerException. I believe that I've done all the correct configuration for the JNDI resource "jdbc/CheckUsers" in server.xml and web.xml.

Does anyone have any ideas as to where to look first in solving this problems?

Thanks,

Ken
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi Ken,

OK, your NullPointerException is being thrown because initContext is null. So you need to look at where the initContext object is created (it's not working ).

If you still have trouble then please post the earlier code that refers to initContext.

Regards

Jules
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Thanks Julian,

I'd forgotten that Eclipse kept giving me an odd error when compiling my InitialContext code. But I moved it to another part of the file and thought that had fixed it. Now that I look I see that it's just not running. However when I put the following code back in Eclipse again isn't happy. The error occurs after the semicolon of the first line and says:
Syntax error on token ";", { expected after this token



This makes absolutely no sense to me but perhaps it does to you.

Thanks for any further thoughts,

Ken
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Again, I think the error is probably earlier in the source. It's likely to be caused by an extra, or missing, curly bracket; missing semicolon; or a nested or unclosed comment. It's usually not too tricky to find these things if you're using a fancy IDE with syntax highlighting.

The normal method for getting to the bottom of these hard-to-find syntax errors is to take a copy of the source file and then just remove section by section, compiling each time, until the error goes away or you spot it. Make sure you don't remove methods that the rest of the code depends on, introducing more errors - just remove the bodies.

Hope this helps.

Jules
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Yes Julian that was my first thought too, that it really was a misleading error report and that the problem was actually elsewhere. I looked for quite a while before deciding that that was not the case. But I think I'll take one more look and get back to you if I still don't find it. My gut tells me that you're right though and that I just didn't find the syntax error earlier.


ken
 
jQuery in Action, 2nd edition
 
subject: InitialContext.lookup always returns NullPointerException
 
Similar Threads
Testing Database Connection
Tomcat Connection Pool
Configuring DataSource on Tomcat5.0
Configuring DataSource on Tomcat5.0
using datasource help