• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jdbc CRUD Operations - Getting Null Pointer Exception

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic