• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting null pointer exception when retrieving value

 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

table rows: Pin#, Account#, Balance

I have added jdbc driver to class path and in JRE system libraries and in JRE lib folder also

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at labtask5.InterfaceDatabase.getCurrentBalance(InterfaceDatabase.java:78)

What's at line 78?
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So you already know that the DriverManager.getConnection() method is throwing an exception. It would be useful to know why that is the case. So instead of just printing the word "catch", which provides no information except the fact that an exception was thrown, put this line of code in the catch block:



That will tell you more about the exception and you can proceed with fixing whatever problem it tells you about.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Both catch blocks here should at the minimum be logging the exception (see Paul's post for how to print the stack trace) and should, to be honest, simply crash out entirely as you can't continue anyway. Possibly throw some form of RuntimeException, wrapping the original exception:

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides the valuable comments of other ranchers I have a few as well.

Let's start by looking at your Connect methodFirst of all, you should not print just "catch" in the catch handler, because that's pretty useless information. You should print (at least) the whole stack trace (like already suggested by Paul and Dave). Secondly, the statement e.getMessage(); is completely useless as well. It will retrieve the message of the exception and then it's immediately "lost", because it's not stored into another variable or printed or ... So you should remove that line as it's useless.

Now we'll take a look at the InterfaceDatabase constructorHere we'll see again the useless e.getMessage(); statement. But you'll be probably using the latest (most recent) MySQL driver. This driver is a JDBC 4.0 driver and one of the enhancements/improvements in JDBC 4.0 is auto-loading of JDBC drivers. You no longer need to explicitly load JDBC drivers using code like Class.forName(), so you can remove the complete constructor from your code. Less is More!

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic