I am learning Java, I wrote this very simple class. Everything is fine until I toward the end. Right after the second showinputDialog
, it continued to another Dialog box from another java file I finished. I am think either I have error in my code, or some setting from my eclipse is off.
// Reads in the radius and length of a cylinder and computes volume
// using the formula: area = radius * radius * PI
// volume = area * length
import javax.swing.JOptionPane;
class ComputeVolumeOfCyclinder {
public static void main(String[] arugs) {
final double PI = 3.1415;
String radiusString = JOptionPane.showInputDialog(
"Enter the radius, \n for example: 15.25");
String lengthString = JOptionPane.showInputDialog(
"Enter the length, \n for example: 50.15");
double radius = Double.parseDouble(radiusString);
double length = Double.parseDouble(lengthString);
double area = radius * radius * PI;
double volume = area * length;
area = (int)area * 100 / 100.0;
volume = (int)volume * 100 / 100.0;
String output = "The area is " + area + "\n The volume is " + volume;
JOptionPane.showMessageDialog(null, output);
}
}
After I existed Eclipse and try to read it again, it shows this error message.
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:989)
at java.lang.Double.parseDouble(Double.java:510)
at ComputeLoanUsingInputDialog.main(ComputeLoanUsingInputDialog.java:14)
Update: I don't know why, but now it seems I can't even run this program. Usually I press the green play button and it will run my program. But after restart, it always run the other program (not what I intended to do). I thought my current program should be one of the options?
A Better answer, as you are running eclipse instead of Run As
you may want to click Debug As
as you see in your picture
Then with step into
(F5) and step over
(F6)
After testing, your code works well if you enter correct values but if you cancel showInputDialog
will return null
you have to check it, and also check that only numeric values are allowed.