This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Jonathan Buchanan
Greenhorn

Joined: Jun 06, 2004
Posts: 1
I am encountering a Nullpointerexception when I try to update a database with:

I have tried many things test or debug that I can't remember, and I can't seem to resaech a solution. Any ideas? The error is occuring on the stmnt.executeUpdate query.

--Added code tags for easier readin - Carl
[ June 07, 2004: Message edited by: Carl Trusiak ]
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
The problem is in your try/catch block. You have created new Connection(con) and the Statement(stmnt) ojbects inside the try/catch block. And if remember object creation inside code blocks{} are a candidate from garbage collection and are out of scope outside of the code block.

Your initial declaration:


Should be changed to ...


I hope this helps.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
It looks like you are inadvertently redeclaring your Connection and Statement objects inside of your try {} block. In your code:

You created a second set of "con" and "stmnt" variables that are available only within the try {} block. Then your update statement code is using the variables that you created above your try {} block which never got initialized, thus the NullPointerException.

Change your code to this:


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
 
 
subject: NullPointerException