File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and Relational Databases and the fly likes Null pointer exception when accessing database 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 "Null pointer exception when accessing database" Watch "Null pointer exception when accessing database" New topic
Author

Null pointer exception when accessing database

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Folks,
Ive been trying to create a mini crud sample using JSP's ,however Im getting a null pointer exception upon trying to insert into the database . Upon checking I found that the request object does contain the passed data, however the same leads to a NPE when inserting to the DB , ive placed the code below. Any help would be great.
Thanks a lot!


Thanks for helping out!


Learning and Learning!-- Java all the way!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62746
    
  68

Moved to the JDBC forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thought it was relevant to JSP forums as it was a JSP page . Anyways , help would be much appreciated as its very very urgent . .Thanks .
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Vic Hood wrote:Hi Folks,
Ive been trying to create a mini crud sample using JSP's

Thanks for helping out!


I know you are just doing a sample, but it is wrong approach to do this inside of a JSP. You are aware this connection stuff should be done in Servlet, right?
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19553
    
  10

You create a Connection variable "con" and assign null to it.

Later you try to use it to create a PreparedStatement. It's still null so you get that exception.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Paul Clapham wrote:You create a Connection variable "con" and assign null to it.

Later you try to use it to create a PreparedStatement. It's still null so you get that exception.

Hi Paul ,
Thanks. For replying . I do understand your point . But can't seem to figure out how to solve it , :( . Could you please suggest a work around, to avoid the NPE ? Thanks a lot .
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Vic Hood wrote: Could you please suggest a work around, to avoid the NPE ? Thanks a lot .


why not get rid of con entirely and just use connection?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks a ton Tim ! That solved the problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null pointer exception when accessing database