• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc CRUD Operations - Getting Null Pointer Exception

 
rajnish patel
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've developed a simple CRUD app. using javabean and getting null pointer exception inUserDao file but can't solve it. Can any one tell me what is the problem ??

this is the code,


User.jsp





UserHandler.java





UserDao.java










this is the error

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception was thrown at dao.UserDao.getAllUsers(UserDao.java:69) so look at that line and see what is null at that point.
You can print out values of variable used at that line to see which one is null. It's most likely the connection that was not assigned.
You should also avoid having a connection as an instance variable. The good practice with connections is get a connection, use it, and close it in a finally. Don't keep it as an instance variable of a long lived object.
 
rajnish patel
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:The exception was thrown at dao.UserDao.getAllUsers(UserDao.java:69) so look at that line and see what is null at that point.
You can print out values of variable used at that line to see which one is null. It's most likely the connection that was not assigned.
You should also avoid having a connection as an instance variable. The good practice with connections is get a connection, use it, and close it in a finally. Don't keep it as an instance variable of a long lived object.




are you saying that problem may be in establishing connection ?? Well I am able to run other programs...
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajnish patel wrote:

are you saying that problem may be in establishing connection ?? Well I am able to run other programs...


No need to guess which one is null. Just print out all variables used on that line before that line is run and see.
 
rajnish patel
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:
rajnish patel wrote:

are you saying that problem may be in establishing connection ?? Well I am able to run other programs...


No need to guess which one is null. Just print out all variables used on that line before that line is run and see.

sorry but I didn't get you....
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this line

is throwing a nullpointer exception then it means that conn was null at that point.
You can confirm this by putting a print out statement like


So now trace back to see why conn is null at that point.
Do not create conn as a private field of the class. Get a new connection in the method that want to use it and close it after the method is done with it in a finally block.
 
rajnish patel
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:If this line

is throwing a nullpointer exception then it means that conn was null at that point.
You can confirm this by putting a print out statement like


So now trace back to see why conn is null at that point.
Do not create conn as a private field of the class. Get a new connection in the method that want to use it and close it after the method is done with it in a finally block.


hmm,
thats the problem, I m getting null as a value of connection... it works on others' system. bt not in mine.
Well, I am able to run other programs...

Now what ??
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajnish patel wrote:
hmm,
thats the problem, I m getting null as a value of connection... it works on others' system. bt not in mine.
Well, I am able to run other programs...

Now what ??

Now, read advice that says
Do not create conn as a private field of the class. Get a new connection in the method that want to use it and close it after the method is done with it in a finally block.
and implement it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic