• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

NullPointerException thrown in JOptionPane

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = "";
try
{
while(!(choice.equalsIgnoreCase("x")))
{
//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();
percent.setMinimumFractionDigits(2);
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)
{
System.exit(0);
}//end catch

System.exit(0);
}//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);
i++;
}//end while

return futureValue;

}//end calculateFutureValue
}//end class
 
John Jaspers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic