This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes JDBC and the fly likes Null pointer exception when accessing database Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Reply 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: 56521
    
  14

Moved to the JDBC forum.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 819

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
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

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: 819

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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Null pointer exception when accessing database
 
Similar Threads
DATABASE LOCKED????
Sequencial ID by Date
Checkbox value not being passed to bean.
Inserting a checkbox value into database.
Error coming 500 please an code is