File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "NullPointerException" Watch "NullPointerException" New topic


Jonathan Buchanan

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"
I agree. Here's the link:
subject: NullPointerException
jQuery in Action, 3rd edition