File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes NullPointerException thrown in JOptionPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException thrown in JOptionPane" Watch "NullPointerException thrown in JOptionPane" New topic

NullPointerException thrown in JOptionPane

John Jaspers

Joined: Mar 27, 2003
Posts: 9
I don't understand why this happens in this code.
The only difference I see is that the first two numbers are doubles and the third is an int, but the exception is caught for the first two and not the third. The third throws the NumberFormatException.
But if I hit Cancel BEFORE parseInt() is called, why am I getting a NumberFormatException?
I couldn't find anything in the docs that might explain this.
Thanks in advance.
import javax.swing.*;
import java.text.*;
public class FutureValue
public static void main(String[] args)
String choice = "";
//Works as expected here.
String paymentString = JOptionPane.showInputDialog(null,
"Enter monthly payment: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
double monthlyPayment = Double.parseDouble(paymentString);

//Works as expected here.
String rateString = JOptionPane.showInputDialog(null,
"Enter yearly interest rate: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
double interestRate = Double.parseDouble(rateString);
double monthlyInterestRate = interestRate/12/100;

//NumberFormatException thrown here.
String yearsString = JOptionPane.showInputDialog(null,
"Enter number of years: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
int years = Integer.parseInt(yearsString);
int months = years * 12;
double futureValue = calculateFutureValue(monthlyPayment,
months, monthlyInterestRate);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String message =
"Monthly payment: " + currency.format(monthlyPayment) + "\n"
+ "Yearly interest rate: " + percent.format(interestRate/100) + "\n"
+ "Number of years: " + years + "\n"
+ "Future value: " + currency.format(futureValue) + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'x': ";
choice = JOptionPane.showInputDialog(null,
message, "Future Value", JOptionPane.PLAIN_MESSAGE);
}//end while
}//end try
catch(NullPointerException e)
}//end catch

}//end main
private static double calculateFutureValue(double monthlyPayment,
int months, double interestRate)
int i = 1;
double futureValue = 0;
while (i <= months)
futureValue = (futureValue + monthlyPayment) *
(1 + interestRate);
}//end while

return futureValue;

}//end calculateFutureValue
}//end class
John Jaspers

Joined: Mar 27, 2003
Posts: 9
I'm sorry. I should have made my question clearer.
The following code is supposed to catch a NullPointerException in any of the JOptionPanes if one presses cancel. This works just fine in the first two, but the third seems to always try to assign to the int. The NullPointerException should occur before the assignment, but it seems to always occur at the assignment and throw a NumberFormatException.
Could anyone explain to me why this happens here and not in the two previous boxes?
Thanks again.
[ March 28, 2003: Message edited by: John Jaspers ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It has nothing to do with JOptionPane as you can see from this:

Any Java source code geeks around?
[ March 28, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: NullPointerException thrown in JOptionPane
It's not a secret anymore!