0

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"))
1
  • 1
    The code you posted here will never compile. Try to at least copy your exact code to the block level, but keep it relevant. In this case, the contents of your for loop. Commented Nov 7, 2013 at 14:04

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

this is the exact mistake (( if( field[row1][column1].equals("t") || field[row1][column1].equals("c") || field[row1][column1].equals("p") || field[row1][column1].equals("r") ||))
Holy crap. Why don't you make that code a bit more readable and add it to your original post? This is trivial to fix, but some context is nice.
i did what you ask me to do.
0

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/

1 Comment

yes i did it but still doesn't work!! for(int leftToPlant=11; leftToPlant>0; leftToPlant--)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.