“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:java中出现0错误

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:java中出现0错误,java,Java,当我试着运行这段代码时,我得到了这个错误。我不知道我哪里出错了 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; // d

当我试着运行这段代码时,我得到了这个错误。我不知道我哪里出错了

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;
        }
    }
}
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:0 位于numericalComputatio.fibo.main(fibo.java:30) 包数值计算; 公共类fibo{ 静态双c=-0.618; //双c=[(1-sqrt(5))/2]=-0.618 /** *计算斐波那契级数 *@param n *@返回 */ 专用静态双fibo(整数n){ 如果(n==0) 返回1; else如果(n==1) 返回c; 其他的 { 双结果=fibo(n-1)+fibo(n-2); 返回结果; } } 公共静态void main(字符串[]args){ int n=0; 双结果=0.0; 双结果1=1.000000000; 如果(参数[0]!=null) n=整数.parseInt(args[0]); 对于(int i=0;i这里:

在线30

if (args[0] != null)
你必须通过辩论

args[0]
尝试访问args数组中的第一个元素,因为该元素是由命令行参数填充的。如果不传递任何参数,则该数组为空,并且尝试访问数组中不存在的元素会导致该异常

你必须学会阅读异常stacktrace。起初它们似乎毫无意义,但一旦你知道如何阅读,它们就会非常有用。这是你的:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at numericalComputatio.fibo.main(fibo.java:30)
它是这样写的:

  • “main”线程中有一个异常,这意味着它直接出现在由
    publicstaticvoidmain
    方法启动的流中
  • 例外情况是:
    java.lang.ArrayIndexOutOfBoundsException:0
    ,这意味着涉及到一个数组,索引试图作为0(第一个元素)进行访问,这为您提供了一个很好的线索
  • 最后打印Java文件的名称和行号:
    fibo.Java:30
    当您手头有源文件时,这也非常有用,可以直接查看行号

我希望这会有所帮助。

args[0]
永远不会为空(从命令行调用时)-但是
args.length
可能为0,在这种情况下,对
args[0]
求值将给出该异常,因为数组中没有元素0。只需测试一下:

if (args.length != 0)
{
    n = Integer.parseInt(args[0])
}

另外,从
fibo
返回一个
double
是非常奇怪的-正常的斐波那契序列是用整数(0,1,1,2,3,5,8等)定义的。如果你想缩放它,我会在之后将它乘以你的常数。

你在运行它时提供了一个命令行参数吗

if (args[0] != null)
    n = Integer.parseInt(args[0]);
如果没有,则上述行将失败。在访问
args[0]
之前,应检查args.length>=1

args[0] != null

args
不包含任何元素,因此尝试访问第0个将给您提供此

以检查应使用的参数。长度-不明确引用索引

此异常表示:

Exception in thread "main" // in main method
java.lang.ArrayIndexOutOfBoundsException: 0 at // Exception ArrayIndexOutOfBounds
抛出以指示数组已 已使用非法索引访问。 该指数为负或负 大于或等于……的大小 阵列

包装车

公共级木质素醇{ 私人点[]索米特

public LignePol(int n) {
    Point[] sommets = new Point[n];
}
public LignePol(Point[] sommets) {
    this.sommets = sommets; 
}

public Point getSommet(int i) {
    return sommets[i];
}
public void setSommet(int i, Point p) {
    sommets[i] = p;
}

public String toString() {
    if (sommets.length == 0)
        return "[ ]";

    String res = "[ " + sommets[0];
    for (int i = 1; i < sommets.length; i++)
        res += ", " + sommets[i];
    return res + " ]";
}

public Object clone() {
    Point[] bis = new Point[sommets.length]; 
    for (int i = 0; i < sommets.length; i++) {
        Point p = sommets[i];
        bis[i] = new Point(p.x(), p.y());
    }   
    return new LignePol(bis);
}

public void homothetie(double k) {
    for (int i = 0; i < sommets.length; i++)
        sommets[i].homothetie(k);
}
public void translation(double dx, double dy) {
    for (int i = 0; i < sommets.length; i++)
        sommets[i].translation(dx, dy);
}
public void rotation(double a) {    
    for (int i = 0; i < sommets.length; i++)
        sommets[i].rotation(a);
}

void tracer() {
    for (int i = 1; i < sommets.length; i++)
        tracerSegment((int) sommets[i - 1].x(), (int) sommets[i - 1].y(), 
                (int) sommets[i].x(), (int) sommets[i].y());
}

public static void main(String[] args) {
    Point[] t = { 
            new Point( 1, 3), new Point( 0, 2), new Point( 0, 0),
            new Point( 3, 5), new Point( 4, 4), new Point( 0, 4),
            new Point( 4, 2), new Point( 4, 0), new Point( 1, 1) };
    LignePol lp = new LignePol(t);

    double m = Double.parseDouble(args[0]); 
    double n = Double.parseDouble(args[1]); 
    double l = Double.parseDouble(args[1]); 

    lp.homothetie(l / 4.0);
    lp.translation(m, n);

    lp.tracer();
}

// pour la simulation
static void tracerSegment(int x0, int y0, int x1, int y1) {
    System.out.println("(" + x0 + "," + y0 + ") --> (" + x1 + "," + y1 + ")");
}
public LignePol(int n){
点[]sommets=新点[n];
}
公共木质素油(点[]sommets){
this.sommets=sommets;
}
公共点getSommet(int i){
返回sommets[i];
}
公共无效设置命令(int i,p点){
sommets[i]=p;
}
公共字符串toString(){
if(sommets.length==0)
返回“[]”;
字符串res=“[”+sommets[0];
对于(int i=1;i(“+x1+”,“+y1+”);
}

}

它不会走那么远,因为它会在测试
args[0]!=null时死掉。由于格式问题,我错过了异常stacktrace。在看到错误行是no.30I后更正了。我没有进入stacktrace,只是知道它在前一行评估args[0]:)那么如何使用命令行测试这样的程序呢?(对不起,我只是个初学者。)你能解释一下你的代码是如何工作的/为什么工作的吗?这将使OP和其他人能够理解并在其他地方应用你的方法(如果适用)。只有代码的答案是可以删除的-
numericalComputatio.fibo.main(fibo.java:30) // in line 30, in class fibo
public class NewMain {

    public static void main(String[] args) {

       int argslen=args.length;
        int argsValue[] = new int[argslen];
        for (String i:args) {
           int d = 0;
           argsValue[d]=Integer.parseInt(i);
           System.out.print(argsValue[d]+"\t");

        }

    }
}
public LignePol(int n) {
    Point[] sommets = new Point[n];
}
public LignePol(Point[] sommets) {
    this.sommets = sommets; 
}

public Point getSommet(int i) {
    return sommets[i];
}
public void setSommet(int i, Point p) {
    sommets[i] = p;
}

public String toString() {
    if (sommets.length == 0)
        return "[ ]";

    String res = "[ " + sommets[0];
    for (int i = 1; i < sommets.length; i++)
        res += ", " + sommets[i];
    return res + " ]";
}

public Object clone() {
    Point[] bis = new Point[sommets.length]; 
    for (int i = 0; i < sommets.length; i++) {
        Point p = sommets[i];
        bis[i] = new Point(p.x(), p.y());
    }   
    return new LignePol(bis);
}

public void homothetie(double k) {
    for (int i = 0; i < sommets.length; i++)
        sommets[i].homothetie(k);
}
public void translation(double dx, double dy) {
    for (int i = 0; i < sommets.length; i++)
        sommets[i].translation(dx, dy);
}
public void rotation(double a) {    
    for (int i = 0; i < sommets.length; i++)
        sommets[i].rotation(a);
}

void tracer() {
    for (int i = 1; i < sommets.length; i++)
        tracerSegment((int) sommets[i - 1].x(), (int) sommets[i - 1].y(), 
                (int) sommets[i].x(), (int) sommets[i].y());
}

public static void main(String[] args) {
    Point[] t = { 
            new Point( 1, 3), new Point( 0, 2), new Point( 0, 0),
            new Point( 3, 5), new Point( 4, 4), new Point( 0, 4),
            new Point( 4, 2), new Point( 4, 0), new Point( 1, 1) };
    LignePol lp = new LignePol(t);

    double m = Double.parseDouble(args[0]); 
    double n = Double.parseDouble(args[1]); 
    double l = Double.parseDouble(args[1]); 

    lp.homothetie(l / 4.0);
    lp.translation(m, n);

    lp.tracer();
}

// pour la simulation
static void tracerSegment(int x0, int y0, int x1, int y1) {
    System.out.println("(" + x0 + "," + y0 + ") --> (" + x1 + "," + y1 + ")");
}