线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:Java中出现0错误 [英] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 error in Java

查看:1999
本文介绍了线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:Java中出现0错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行此代码时,我收到此错误..我不知道我哪里出错..

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
    at 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 the 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 i = 0; i<=n; i++)
        {
            result = fibo(i);
            System.out.println("fib(" + i + ") = " + result + "Formula value = " + result1);
            result1 = result1*c;
        }
    }
}


推荐答案

这里:

 args[0]

第30行

if (args[0] != null)

你必须传递一个参数。

args [0] 尝试访问args数组中的第一个元素,因为它是从命令行参数填充的。如果您没有传递任何数组为空的参数,并且尝试访问数组中的非现有元素会给出该异常。

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)

它如下所示:


  • 你在主线程中有一个例外,这意味着它直接来自 public static void main 方法

  • 例外是: java.lang.ArrayIndexOutOfBoundsException:0 这意味着涉及到一个数组,并尝试了索引作为0(第一个元素)的访问,它为你提供了一个很好的线索。

  • 最后打印Java文件的名称和行号: fibo.java:30 当你手头有这个源文件时,这也非常有用,并且可以直接查看该行号。

  • 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.

我希望这会有所帮助。

这篇关于线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:Java中出现0错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆