File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException At Statement stmt = conn.createStatement();

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use a connection pool to retrieve
data from a database. I have succeeded in putting InitialContext, DataSource, Statement,
ResultSet in "one servlet". And I am able to display the output in the browser. It means that the table is created in the database, the connection pool is properly configured, DataSource is referenced in the web.xml, and mapping in the web.xml has been taken care of.
I ran into problem when I tried to put
IntialContext and DataSource in one class
(DBConnection.java) that specializes in providing Connection object (conn). And another class
(VerificationServlet.java) with Statement and
ResultSet in it references the provided Connection object. (DBConnection.java and VerificationServlet.java are in a package 'cptest'.)
The error message I got is
"java.lang.NullPointerException at
cptest.VerificationServlet.doGet(VerificationServlet.java:25)
and the 25th line of the VerificationServlet is
Statement stmt = conn.createStatement();
I am lost. I do not know what went wrong.
DBConnection.java
VerificationServlet.java

[ July 11, 2003: Message edited by: JiaPei Jen ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you've forgotten to get the Connection from the DataSource.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, my bad.
What happens if conn = ds.getConnection() throws an exception? You aren't even checking for it.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is caught in the code snippet shown below:

Somehow, the Connection (conn) is not passed from DBConnection.java to VerificationServlet.java. Just cannot figure out what wrong with my code.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an exception is thrown, you hide the exception and return null. Are you sure there is no exception being thrown?
This would cause a NullPointer every time.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic