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 JDBC query working. Servlet with same code throwing NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC query working. Servlet with same code throwing NullPointerException" Watch "JDBC query working. Servlet with same code throwing NullPointerException" New topic
Author

JDBC query working. Servlet with same code throwing NullPointerException

michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
I successfully tested JDBC connection of SQL Server with a small example. But when I incorporated the same code in a Web application it is throwing nullpointerexception.
Can anyone tell me how to overcome this? Thanks.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Which line is the NullPointerException occuring on? NullPointerExceptions always tell you the line number; check what is happening there and think about what could be null and why.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
Line number indicates ResultSet declaration statement i.e., Since this query was already tested, I am unable to figure out how the ResultSet object is null.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by michael tall:
Line number indicates ResultSet declaration statement i.e., Since this query was already tested, I am unable to figure out how the ResultSet object is null.


No, that indicates that it it statement that is null. Given you are creating this in your init() method, chech the logs when the servlet was initialised to find out more.

(NB: why are you using the init() method to create your statement?)
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
I wanted to get the database connection strings from deployment descriptor. From init() I can access those strings through ServletConfig object.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That's OK, but what you have defined is one single connection and statement for all requests to your servlet.
[ November 05, 2008: Message edited by: Paul Sturrock ]
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
I wanted to see a simple servlet from a small web application work. Just to get the init parameters and connect to database. Part of learning for me.
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
It's taking strings from deployment descriptor. Probably failing at connection. But can't figure out why?
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
I changed the code. It threw following exception: ClassNotFoundException com.microsoft.sqlserver.jdbc.SQLServerDriver
Same thing worked in a non-servlet class
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Originally posted by michael tall:
I changed the code. It threw following exception: ClassNotFoundException com.microsoft.sqlserver.jdbc.SQLServerDriver
Same thing worked in a non-servlet class


Now its a search path issue. You are using a third party driver whose class files cannot be located in the search path. Its a bit different when it comes to web applications.

If you have a jar file of the driver, put it in your WEB-INF/lib directory.

For testing purpose, try using the JDBC-ODBC bridge driver.


Hope this helps

[Edit: fixed typing mistake]
[ November 05, 2008: Message edited by: Amit Ghorpade ]

SCJP, SCWCD.
|Asking Good Questions|
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
Can you please make it clearer? Which WEB-INF directory? There is no lib subdirectory in WEB-INF. Thanks
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
You are again correct. I created lib subdirectory under my web applications WEB-INF directory and copied the jar file into WEB-INF\lib. It worked. Do I have to repeat the copying act for each and every web application? Is there a workaround? Thanks any way. Bigger problems will be dealt as they are faced
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes. If you are going to use third party libraries in your web application you will need to make sure they are available in the classpath. The mechanism the servlet specification allows for this is the WEB-INF/lib directory.

However, normally in a web application, you would use a DataSource which is managed by the Servlet container and so the driver files would go in the container's classpath not your web application's.
michael tall
Ranch Hand

Joined: Nov 02, 2008
Posts: 61
I placed the jar file of the driver class in lib directory under Tomcat installation directory. Then I removed the jar file I had in WEB-INF/lib directory. It is working fine. Till I learn how to configure a datasource on Windows XP, this should be enough. Thanks.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

That is because that lib directory is Tomcat's classpath. So any jar files you put in there are on the classpath for Tomcat and any web app you deploy on it.

If you were using a DataSource rather than the DrievrManager your driver files would go here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC query working. Servlet with same code throwing NullPointerException