user2965010
user2965010

Reputation: 1

array (java.lang.ArrayIndexOutOfBoundsException:-1)

what is this mistake mean? java.lang.ArrayIndexOutOfBoundsException: -1 ?

java.lang.ArrayIndexOutOfBoundsException: -1
    at Game.Game.plantVegetables(Game.java:1160)
    at Game.__SHELL11.run(__SHELL11.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:725)

Exception occures at:

   Scanner keyIn = new Scanner (System.in);

   for(int leftToPlant=10; leftToPlant>0; leftToPlant--)
      if (field[row1][column1].equals("t") ||
          field[row1][column1].equals("c") ||
          field[row1][column1].equals("p") ||
          field[row1][column1].equals("r"))

Upvotes: 0

Views: 946

Answers (2)

ZmG
ZmG

Reputation: 1

Assuming you have an array in the for loop, you are trying to access an element in the array with an invalid index. Check your for loop and make sure that your array contains 11 elements. First element will not be accessed.

Sample Code in JavaScript with Array of 10 elements

    cars=["BMW","Volvo","Saab","Ford","SD","BMW","Volvo","Saab","Ford","SD"];

    for (var i=10;i>0;i--)
    {
     document.write(cars[i]);
    }

Output:

     undefined    - Element 10 
     SD           - Element 9 
     Ford         - Element 8
     Saab
     Volvo
     BMW
     SD
     Ford
     Saab          - Element 2
     Volvo         - Element 1

  Element 0 won't be access because your loop stops at 1.

Fiddle: http://jsfiddle.net/VFLLN/

Upvotes: 0

Sietse van der Molen
Sietse van der Molen

Reputation: 705

You're trying to take the -1'th element of an array where it doesn't exist. Post more code for a more exact answer.

Upvotes: 2

Related Questions