Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null pointer exception when accessing database

 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Tim ! That solved the problem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic