aspose file tools*
The moose likes JDBC and the fly likes NullPointerException in PreparedStatement... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NullPointerException in PreparedStatement..." Watch "NullPointerException in PreparedStatement..." New topic
Author

NullPointerException in PreparedStatement...

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi,
Things looking weird in Java, Yes, Ofcourse when i run this code yesterday it works fine and now it shows me a NPE on the PreparedStatement line.. Here is my code...

: Edited :


Never try to be a hard-worker. Be a smart-worker.
My Blog
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So "con" is null. Where is is set?
[ July 23, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

The con is set in dbmodel.java and i extends that file with this file inorder to access the DB methods such as Statement, PS and con etc....
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

...and how/when is it set? Something is going wrong with how you use it. Either there has been a problem establishing a connection (are there any stacktraces?) or you are using it incorrectly (e.g. there is an open connection step you are missing).
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Here is the stackTrace of errors...



vedha vas
Greenhorn

Joined: Jul 22, 2008
Posts: 10
Hi Rajkumar balakrishnan,

The problem is with the con.Con is null.
The connection is not estabilished thats why its throwing NPE.
Pl post the dbModel.java class also so that we can find out how you are estabilishing the connection.


Regrads,<br />Vedha.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So that is your NullPointerException. Well, we already know what causes that: the variable "con" is null. If there are no other exception messages then we can assume you are doing something wrong how you use con. Can you show use the code where con is initialised?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Insert this line just before it:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
Oh, I had this page open for some time, and didn't realise you had so many useful replies before mine! Sorry.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

: Edited :
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So presumably you have actually called your connect() method before you call your failing method?
vedha vas
Greenhorn

Joined: Jul 22, 2008
Posts: 10
Hi Rajkumar balakrishnan ,

In the checkSession method, in the try part first call the valueBound(event) method after that start
with the code:
ps=con.prepareStatement(SQL2);
ps.setString(1,sname);
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Small mistake.....
Thanks for your reply....
Its the closing time for this thread...
Meet you in some other post...

Thanks for all ranchers....
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by vedha vas:
Hi Rajkumar balakrishnan ,

In the checkSession method, in the try part first call the valueBound(event) method after that start
with the code:
ps=con.prepareStatement(SQL2);
ps.setString(1,sname);


You should not be calling these methods directly. DBModel implements HttpSessionBindingEvent so will be called when a session binding event occurs. Presumably the pattern is to open a single connection for a single user when their session is bound?
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Yes.... Ofcourse....Thats's right...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException in PreparedStatement...