• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Getting connection error. Beginner question.  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I have posted this in the right forum category. I get this error

Exception in thread "main" Connection failed....
java.lang.NullPointerException
at com.company.DB_Connection.getEmployeesName(DB_Connection.java:23)
at com.company.Main.main(Main.java:8)

Process finished with exit code 1
When trying to create two simple methods. Is there anything wrong with my methods or with my connection? Please explain it to me like I am five years old because I don't have much experience using Java with MySQL.



Thanks in advance.
 
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your main method? And do you add external jar to your build path?
 
Elias Gomersen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:Where is your main method? And do you add external jar to your build path?


Here is my main
I have added the connector file to intelli, following youtube tutorial.
 
Shubham Rai
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run this code on my machine it works fine.But since your exception is pointing to line 8 and 23.Check that database name ,username and password must be correct for mysql server and also the spellings of table name as employees and column names as firstname and lastname are correct and exists in your database
 
Elias Gomersen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shubham Rai wrote:I run this code on my machine it works fine.But since your exception is pointing to line 8 and 23.Check that database name ,username and password must be correct for mysql server and also the spellings of table name as employees and column names as firstname and lastname are correct and exists in your database



Can you please send me a screenshot of the run? I have this seminar tomorrow and I want to show that my code is not wrong but could be changed. So if you don't mind sending me a screenshot of the code not getting the error I would appreciate it.
Staff note (Knute Snortum):

Please don't share things privately; share them on the jorum. That's what we're all here for.

 
Rancher
Posts: 3931
45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than this:


print the exception with stack trace:


That will tell you exactly what the issue is.

I would recommend doing the same with the other cath blocks in this code.
 
Elias Gomersen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Rather than this:


print the exception with stack trace:


That will tell you exactly what the issue is.

I would recommend doing the same with the other cath blocks in this code.



Thank you!!
I got this now, does this mean my connector that I got from my teacher is not right?

java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/world?user=root&password=root
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.company.DB_Connection.<init>(DB_Connection.java:13)
at com.company.Main.main(Main.java:7)
Exception in thread "main" java.lang.NullPointerException
at com.company.DB_Connection.getEmployeesName(DB_Connection.java:23)
at com.company.Main.main(Main.java:8)

Process finished with exit code 1
 
Master Rancher
Posts: 1162
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As someone already sugested you need a mysql driver.
What kind of project is this, I mean what technologies are you using?
If maven, gradle or any other kind of build tool you can add the dependency, if not you most likely will have to add it to your classpath yourself.
What version of mysql are you using?
 
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to add the driver by hand, here is where you would get it:

https://dev.mysql.com/downloads/connector/j/

(The term "driver" is a little outdated; try searching the web for mysql java connector)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!