• 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

Database Connection is Null but why?

 
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

My current lab / homework has me stumped. In my class file name LocalMySQL.java It seems like the database connection is null but I don't understand why it's null.

Everything I do to try and test this seems to show that a connection is being made. I have a servlets ( name ServletEED) which should use the connection information found on LocalMySQL class. It is returning the error I created for when the connection isn't available.

In my class file I added a System.out.print("THIS WORKS ") to print out "THIS WORKS " thinking that this would only print out if the connection was being made. I guess that's not the case? "THIS WORKS" prints out to my console but it's followed by "Error" which I added in the Main method.

I created a servlet that connects to the same databases and table and was able to run a sql statement via. a servlet and the sql statement worked. My only thoughts is perhaps the connection object created in openDB() is being closed right after it is called? My other thought is perhaps I just don't really understand what a connection object is and my error checking is wrong.

Help please. How do I maintain this connection I need or why is it not available to my ServletEED? Ultimately I need my ServletEED to be able to establish a connection using my LocalMySQL class file . As it is right now I'm not able to move forward with the rest of my lab.

Thank You all so much!



Here is the LocalMySQL class







Here is the servlete which uses the LocalMySQ class file.




This is the servlet I created to query my database and it works.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You declared a "Connection" variable at line 11, and I think you're using that variable in the line of code where you get the NullPointerException. But it's null, as the exception tells you. And that's because you never assign a value to that variable.

You do assign a value to another variable with the same name, but it is a local variable in one of the methods. So you're shadowing your instance variable with a local variable of the same name. You should be able to fix your problem by assigning the Connection object reference to the instance variable instead of the local variable.

By the way if you close a database connection, that just changes the state of the database connection. There may be variables containing references to the connection, but closing the connection can't possibly set those variables to null. Using a closed connection would produce a different exception.
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You declared a "Connection" variable at line 11, and I think you're using that variable in the line of code where you get the NullPointerException. But it's null, as the exception tells you. And that's because you never assign a value to that variable.

You do assign a value to another variable with the same name, but it is a local variable in one of the methods. So you're shadowing your instance variable with a local variable of the same name. You should be able to fix your problem by assigning the Connection object reference to the instance variable instead of the local variable.

By the way if you close a database connection, that just changes the state of the database connection. There may be variables containing references to the connection, but closing the connection can't possibly set those variables to null. Using a closed connection would produce a different exception.



Oiy that makes total sense now that it's pointed out to me. I removed line 72 and it worked. This is also probably why when I run ServletEED it says it's null the this.connection in the isAvailable() method would be null. I need to set the this.connection to the value (Connection) DriverManager.getConnection(dbURL,username, password); but that can't be set in the class field. I need to figure this part out but Thank you for your help!
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I changed my isAvaiable() method to



The results seem to be what I expect. I can move forward I hope. Again THANK YOU. I'm so blind at time.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic