Originally Posted by
alex067
No need to act cocky
I apologize for my use of sarcasm. I can see that it didn't add anything useful to the conversation. I should have known better. I will try to do better.
I can also see that asking you what you didn't understand was an extremely stupid thing to say. I mean that was a stupid question on my part. Sorry.
The intent of my suggestions was the following: If you are getting a NPE as a result of a particular statement in your program, then, first of all look, at the code on the line was flagged. If the statement extends over multiple lines, then look at the entire statement.
Then, if you can't see how anything in that entire statement can be causing the specific error that was reported, you can put print statements
immediately before that statement to try to let the program itself show you which variable had a null value at the time the NPE occurred.
Putting print statements in
other places in the code might not help much.
In particular...
Putting print statements in parts of the code
that were not executed during a run that results in an NPE definitely will not help find the cause of that NPE
for that run.
Putting print statements
after the statement that cause the NPE for that run definitely will not help find that cause of the NPE
for that run.
Of course, the entire point is that just finding which variable had a null value is not the object of the exercise, but it might make you go back and look at the code again with a specific focus of finding where that variable was declared and where you think it was given a value that would make it not be null. Apparently you were able to find out what caused the NPE that you previously reported.
OK. If we can get past my previous bad behavior, let's get on to your next problem:
Originally Posted by
alex
Error:
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1008)
at java.lang.Double.parseDouble(Double.java:540)
at assignment1.Assignment1.getTransAmt(Assignment1.ja va:133)
at assignment1.Assignment1.main(Assignment1.java:57)
Java Result: 1
.
.
.
The line where it states: tamount = Double.parseDouble(transamount);
OK, let's go through it again.
First of all, you didn't tell us what you did to make the program give the NPE.
I will make an assumption here. You fixed the problem about the NPE due to info not being initialized, otherwise you wouldn't have gotten this far.
Now, I think that you did the following sequence:
Executed the program.
The program asked for an initial balance. You entered a number and clicked 'OK'. If you had done something invalid, there would have been an error at that point.
It asked for a transaction code. You entered
1 and clicked 'OK'. That's the only way that I can see that you got to the point in your program where Something Bad happened
Now, we get to the point where the NPE occurred.
The dialog box asks you to "Enter a transaction amount"
It waits until you close the dialog box. I can think of four things that you could do here to close the dialog box. It might be worthwhile to investigate:
What happens if you enter a number in the dialog box and click "OK"?
What happens if you enter something other than a number and click "OK"?
What happens if you enter nothing in the window and click "OK"?
What happens if you close the dialog box by clicking "Cancel" or by doing anything other than clicking "OK"?
Did any of things make an NPE occur at this point in the program?
Here is my suggestion:
Go back into your program
where this NPE occurs and put a print statement after the call to the showInputDialog() method and before you try to parse the String to obtain a number:
public static double getTransAmt()
{
double tamount;
String transamount;
transamount = JOptionPane.showInputDialog ("Enter your trans amount:");
// New statement for debugging: What happens when you click "Cancel"
// or close the dialog box by any method other than clicking "OK"
System.out.println("transamount = " + transamount);
tamount = Double.parseDouble(transamount);
return tamount;
}
Now, if you don't get this far in the program, it won't print anything out, right? So, follow whatever sequence you executed to get the message that you reported and see what happens here.
Cheers!
Z