Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
I have an assignment due and i cannot get this to work.
public class InitArray
public static void main( String args[ ] )
// check number of command-line arguments
if ( args.length != 3 )
"Error: Please re-enter the entire command, including
"an array size, initial value and increment." );
// get array size from first command-line argument
int arrayLength = Integer.parseInt( args[ 0 ] );
String args1 = new String[ arrayLength ]; // create array
// get initial value and increment from command-line argument
double initialValue = Double.parseDouble( args[ 1 ] );
double increment = Double.parseDouble( args[ 2 ] );
double total = 0.0;
for ( String argument : args1 )
total = total + Double.parseDouble( argument ) * initialValue + increment;
System.out.printf( "The sum of the values in the array are %f", total );
} // end main
} // end class
i have to use the enhanced for statement to calculate the sum of the array. the values are passed using the command-line. here is the error message that i get.
C:Java>java InitArray 3 1 5
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
any help would be appreciated. thanks.