Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java

前端 未结 8 1994
清酒与你
清酒与你 2021-01-07 05:52

When i tried running this code I get this error..I dont know where i went wrong..

Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: 0
           


        
8条回答
  •  隐瞒了意图╮
    2021-01-07 06:06

    Here:

     args[0]
    

    On line 30

    if (args[0] != null)
    

    You have to pass an argument.

    args[0] Tries to access the first element in the args array, since which is filled from the command line arguments. If you don't pass any arguments that array is empty and trying to access an non existing element in an array gives that exception.

    You have to learn to read the exception stacktrace. They seem meaningless at first, but once you know how to read it, they are very helpful. Here's yours:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at numericalComputatio.fibo.main(fibo.java:30)
    

    It reads like this:

    • You have an exception in the "main" thread, which means it comes directly in the flow started by the public static void main method
    • The exception was: java.lang.ArrayIndexOutOfBoundsException: 0 which means there there is an array involved and the index tried to be access as 0 ( the first element ) that gives you a good clue of what's going on.
    • Finally the name of the Java file and the line number is printed: fibo.java:30 This is also very helpful specially when you have that source file at hand, and can look directly at that line number.

    I hope this helps.

提交回复
热议问题