Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

NullPointerException At Statement stmt = conn.createStatement();

 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic