• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

How to Resolve Java / MySql error: Driver loaded, but cannot connect to db: jdbc:mysql://ecterra

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am finishing a program and I am having trouble getting the database to connect properly. I just completely removed and reinstalled MySQL so I don't think the error is caused by a bad password. Thanks for your help!

The error I'm getting is:

Driver loaded, but cannot connect to db: jdbc:mysql://localhost:3306/registrar
Exception in thread "main" java.lang.NullPointerException
at student.Undergraduate.add(Undergraduate.java:156)
at student.StudentAppDriver.addStudent(StudentAppDriver.java:102)
at student.StudentAppDriver.start(StudentAppDriver.java:30)
at student.StudentAppDriver.main(StudentAppDriver.java:20)

Code:
 
Marshal
Posts: 26697
81
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

Matthew Blake wrote:Driver loaded, but cannot connect to db: jdbc:mysql://localhost:3306/registrar
Exception in thread "main" java.lang.NullPointerException
at student.Undergraduate.add(Undergraduate.java:156)
at student.StudentAppDriver.addStudent(StudentAppDriver.java:102)
at student.StudentAppDriver.start(StudentAppDriver.java:30)
at student.StudentAppDriver.main(StudentAppDriver.java:20)



That's a stack trace. One of its purposes is to tell you exactly where the exception was thrown. In this case, starting from the top, it was thrown at line 156 of the "student.Undergraduate" class, which is inside a method called "add". And that line of code was called from line 102 of the "student.StudentAppDriver", which is inside a method called "addStudent".

You posted some other code, but that's just because your guess about the source of the problem wasn't correct. So now that you have something pointing to the actual problem, you should start by looking at that code instead. As always, if you have more questions, we're always here.
 
Sheriff
Posts: 22264
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The NPE probably stems from result never being initialized, as that step fails.

Matthew, can you add e.printStackTrace() after line 36 and print the results? Because you're getting an SQLException, but you ignore the exception itself. You shouldn't, as it contains the reason why the SQLException occurs.
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul: I took a look at the lines mentioned but didn't see anything wrong with the code. Netbeans is not throwing any errors at me either.

Rob: I added e.printStackTrace() after line 36, but there is nothing different in the output box. Should I be looking somewhere else for the information?

Additional code from class StudentAppDriver:
 
Paul Clapham
Marshal
Posts: 26697
81
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

Rob Spoor wrote:The NPE probably stems from result never being initialized, as that step fails.



Ah, I get it now... the error message comes from the posted code, and the stack trace comes from something which calls it but fails because it doesn't return a Connection properly.
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob: any idea how to fix it?
 
Paul Clapham
Marshal
Posts: 26697
81
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
There's a design problem with your getSimpleConnection() method. It should always return a Connection object; returning null, as it is now, is a bad thing. So in particular it should either return a valid Connection or it should throw an exception explaining why not. Ignoring the error, as you basically do now, is not a good idea.

And as Rob says, when you do catch an exception, you shouldn't ignore it and display some generic method. The exception tells you what went wrong, so printing its stack trace is a good idea.
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for sticking with me, I struggle with programming, but hopefully I can get my head around this.

I made a couple changes. The first is I added a connection just under getSimpleConnection() with: Connection conn = null
At the end, just above the first try/catch I placed: conn = DriverManager.getConnection(DB_CONN_STRING, USER_NAME, PASSWORD);
I also have e.printStackTrace() at line 38. Did I implement it correctly?
Lastly, I removed result completely and replaced it with conn throughout the code
This seems to have no effect though. I must be missing some a concept somewhere.
Here is my current code with the updates I made:
 
Matthew Blake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally got this working. It appeared to be a few different problems ranging from proper login credentials to needing to revise my database table. Thanks for your help gentleman!
 
Rob Spoor
Sheriff
Posts: 22264
119
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic