• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

NullPointerException thrown in JOptionPane

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 = "";
//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
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 ]
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic