开发者

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

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开发者_JS百科*c;
        }
    }
}


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.


To check the args you should use args.length - not reference the index explicitly


args[0] will never be null (when invoked from the command line) - but args.length may be 0, in which case evaluating args[0] will give you that exception, because there is no element 0 in the array. Just test for that:

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

As an aside, it's pretty odd to return a double from fibo - the normal Fibonacci sequence is defined in terms of integers (0, 1, 1, 2, 3, 5, 8 etc). If you want to scale it, I'd multiply it by your constant afterwards.


Are you supplying a command line argument when you run it?

if (args[0] != null)
    n = Integer.parseInt(args[0]);

If you aren't then the above line will fail. You should check if args.length >= 1 before accessing args[0].


args[0] != null

args doesn't hold any element , so trying to access 0th will give you this


This exception means:

Exception in thread "main" // in main method
java.lang.ArrayIndexOutOfBoundsException: 0 at // Exception ArrayIndexOutOfBounds

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

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");

        }

    }
}


package coordCart;

public class LignePol { private Point[] sommets;

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 + ")");
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜