-1

What does -1 mean in this error code?

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1

oorkyy
  • 57
  • 1
  • 1
  • 7
  • That you've tried to access an array position that does not exist. For example, if the array has 10 elements (positions 0 to 9), and you try to use the 11th position, or less than 0 (which seems like the -1 in this case), you are out of bounds. – Paul T. Apr 06 '19 at 04:40

2 Answers2

3

Per the Java Documentation, an ArrayIndexOutOfBoundsException is "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."

This usually occurs when you try to access an element of an array that does not exist. Consideer the following example:

String[] items = {"One", "Two"};
System.out.println(items[3]);

This will throw an ArrayIndexOutOfBoundsException because there is no items[3] element.


Since you've not posted any code, we cannot help with your specific situation, but this should lead you in the right direction.

Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • This is the line it is take from JButton button = board[x + offset][y]; There is too much code to put the rest on here. – oorkyy Apr 06 '19 at 04:45
  • Then I suggest you read the help section on how to provide a [mcve]. In many cases, the process of creating the MCVE will help you discover the error anyway. – Zephyr Apr 06 '19 at 04:47
  • 1
    There are literally an infinite number of reasons you could receive this error. We have no idea what `board` or `x` or `offset` or `y` might be. – Zephyr Apr 06 '19 at 04:48
2

The index out of bounds means you have tried to get something from an array or list with an invalid index. The -1 is most likely the index you gave it. An array or list will never have an index of -1 be valid.