Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 error in Java -
when tried running code error..i dont know went wrong..
exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 @ numericalcomputatio.fibo.main(fibo.java:30) package numericalcomputatio; public class fibo { static double c = -0.618; // double c = [(1-sqrt(5))/2] = - 0.618 /** * computes fibonacci series * @param n * @return */ private static double fibo(int n){ if (n == 0) return 1; else if (n == 1) return c; else { double result = fibo(n - 1) + fibo(n - 2); return result; } } public static void main(string[] args) { int n = 0; double result = 0.0; double result1 = 1.000000000; if (args[0] != null) n = integer.parseint(args[0]); for(int = 0; i<=n; i++) { result = fibo(i); system.out.println("fib(" + + ") = " + result + "formula value = " + result1); result1 = result1*c; } } }
here:
args[0] on line 30
if (args[0] != null) you have pass argument.
args[0] tries access first element in args array, since filled command line arguments. if don't pass arguments array empty , trying access non existing element in array gives exception.
you have learn read exception stacktrace. seem meaningless @ first, once know how read it, helpful. here's yours:
exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 @ numericalcomputatio.fibo.main(fibo.java:30) it reads this:
- you have exception in "main" thread, means comes directly in flow started
public static void mainmethod - the exception was:
java.lang.arrayindexoutofboundsexception: 0means there there array involved , index tried access 0 ( first element ) gives clue of what's going on. - finally name of java file , line number printed:
fibo.java:30helpful specially when have source file @ hand, , can directly @ line number.
i hope helps.
Comments
Post a Comment