I am new to
Java and trying to input 6 numbers in the JOptionPane and store those in a
string array. I then need to convert those into doubles and find the largest and smallest then dispaly that in an
applet.
The issue I am having is that I get this error when I try to run the program:
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:991)
at java.lang.Double.parseDouble(Double.java:510)
at Eagle.init(Eagle.java:32)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
Any help would be greatly appreciated.
Here is my code:
import javax.swing.JApplet; // program uses class JApplet
import javax.swing.JOptionPane; // program uses class JOptionPane
public class MinMax extends JApplet
{
// initialize applet by obtaining values from user
public void init()
{
//Create string array with six places
String[] sArray = new String[6];
//create double array same length as string array
double [] dArray = new double [sArray.length];
//create for loop to store six numbers read in from JOptionPane
for (int i = 0; i < sArray.length; i++) {
sArray[0] = JOptionPane.showInputDialog("Enter a number" );
}
//create a for loop to convert string array to double
for(int i = 0; i < sArray.length; i++)
{
dArray[i] = Double.parseDouble(sArray[i]);
}
//create for loops to determine smallest and largest values
double max = dArray[0];
for (int i=0; i<dArray.length; i++){
if (dArray[i] > max){
max = dArray[i];
}
}
double min = dArray[0];
for (int i=0; i<dArray.length; i++){
if (dArray[i] < min){
min = dArray[i];
}
}
//report the largest and smallest numbers entered in a JOptionPane
JOptionPane.showMessageDialog(null,
"The number with the smallest value is " + min
+ "\nThe number with the largest value is " + max);
}
}