tags:

views:

79

answers:

3

Hello,

I have a strange problem which I can't fix:

A field:

private boolean[][][] gaps;

Constructor (1st line):

gaps = new boolean[NOBARRICADES][WIDTH][HEIGHT];

Constructor (2nd line):

for (int i = 0; i < NOBARRICADES; i++) {

Java throws an error for the 2nd line, saying:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

Does it have anything to do with Java syntax (the mistake is in these lines of code) or I should look for the problem somewhere else?

+1  A: 

You're probably misreading the error output. Your second line does not even access the array - make sure that it's not the first line of the body of the for-loop that throws the exception. Also, make sure that you use i only to index the first dimension of your array.

Thomas
A: 

sometimes the java compiler is off by a line or two. You may check the lines of code around the line that it says the error is on and see if you see anything.

Joel
When? In my experience the java compiler is NEVER off by "a line or two". The only difference would be if you downloaded the source file from the server it was running on and the FTP client you used inserted line breaks to format it differently.
KG
I have this problem when I use the Java-source classes. I think it's a problem with the javadoc. The runtime gives a line number and if I check that line, it is from a completely other method.
Martijn Courteaux
A: 

Sorry, but you really don't want to do that.

Multidimensional arrays are never worth the confusion they cause--they have no positive value at all (with the POSSIBLE exception of a clear, obvious x,y array).

I suggest you try starting with either a list of two-dimensional arrays or a two-dimensional array of objects where each object contains a list.

Bill K