Forums Register Login

getting null pointer exception when retrieving value

+Pie Number of slices to send: Send

table rows: Pin#, Account#, Balance

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

+Pie Number of slices to send: Send
 

at labtask5.InterfaceDatabase.getCurrentBalance(InterfaceDatabase.java:78)


What's at line 78?
+Pie Number of slices to send: Send


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.
+Pie Number of slices to send: Send


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:

+Pie Number of slices to send: Send
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
Why fit in when you were born to stand out? - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 632 times.
Similar Threads
Error in Connection
Establishing JDBC Connection Using Port Ips
Error accessing a static method (JSP, Java)
Null Pointer Exception
Transfer Function on the verge of working
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:03:39.