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 main method
  • the exception was: java.lang.arrayindexoutofboundsexception: 0 means 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:30 helpful specially when have source file @ hand, , can directly @ line number.

i hope helps.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -