Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

NullPointerException

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Dinner will be steamed monkey heads with a side of tiny ads.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic