Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC query working. Servlet with same code throwing NullPointerException

 
michael tall
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
michael tall
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to get the database connection strings from deployment descriptor. From init() I can access those strings through ServletConfig object.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's taking strings from deployment descriptor. Probably failing at connection. But can't figure out why?
 
michael tall
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
michael tall
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please make it clearer? Which WEB-INF directory? There is no lib subdirectory in WEB-INF. Thanks
 
michael tall
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic