• 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

NullPointerException - connecting it mySQL

 
Jeremiah Elliott
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys, here is the deal: I am a student, who is tryin to learn java. If there is a good tutorial some where,just post a link and I will be glad to RTFM. I am trying to connect to a mySql database from tomcat, but get the following error message:
The code for the connection class is here:
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe your problem code is here:
What your code is doing is:
if connection is null then perform "statement = connection.createStatement();" in which connection is null. You do create a new Connection called "conn" if connection is null, but never use it ( you use the old null connection )
Jamie
 
Jeremiah Elliott
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip, however it has not solved the problem. this is what I have done:

the error is on line 51, based on the tomcat error message. line 51 is:
return statement.executeQuery(sql);
I also tried this code where you where saying the error was:

but it had the same results.
thanks for your time, Jeremiah
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeremiah: This line of code worries me. Maybe someone can verify my thoughts.

If I am right, both conditions get evaluated every time. If this is the case, the order of events would be 1. connection == null evaluates to true 2. It tries to evaluate connection.isClosed() but since it is null, it throws a null pointer exception. maybe you can rework your code like this:

This would ensure that your connection is not null before you call the method isClosed().
let me know if this is the case
Jamie
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, imho, the line you mention uses the logical short-circuit operator and, as such, should never try to invoke the method of connection if the value of connection is null:
if (connection == null || connection.isClosed()){
the short-circuit "or" essentially says "if the first statement is true, just stop looking and return true." So, if it is the case that the value of connection is null, evaluation of the boolean immediately stops and returns true.
had the statement been:
if (connection == null | connection.isClosed())
I think your concern would be valid.
 
Bosun Bello
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep! You are corret Dave. The second condition only gets evaluated if the first one is false.
 
Jeremiah Elliott
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone!!
I got it working now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic