wood burning stoves 2.0*
The moose likes JDBC and the fly likes NullPointerException - connecting it mySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "NullPointerException - connecting it mySQL" Watch "NullPointerException - connecting it mySQL" New topic
Author

NullPointerException - connecting it mySQL

Jeremiah Elliott
Greenhorn

Joined: Nov 01, 2001
Posts: 17
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

Joined: Jul 09, 2001
Posts: 1879

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

Joined: Nov 01, 2001
Posts: 17
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

Joined: Jul 09, 2001
Posts: 1879

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

Joined: Mar 26, 2002
Posts: 262
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.


Fun programming etcetera!
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Yep! You are corret Dave. The second condition only gets evaluated if the first one is false.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Jeremiah Elliott
Greenhorn

Joined: Nov 01, 2001
Posts: 17
Thanks everyone!!
I got it working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException - connecting it mySQL
 
Similar Threads
Error handling in JSP
bean returning error on sql
JDBC Driver not found.
Why am I getting this error!!!!
connecting to database!