-2

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15" is the error message I get when running my code, I assume it is trying to use a number which is larger than the array or something along those lines; heres my code:

public class TileGrid {

public Tile[][] map;

public TileGrid(){
    map = new Tile[20][15];
    for(int i=0; i<map.length; i++){
        for(int j=0; j<map[i].length; j++){
            map[i][j]=new Tile(i*64, j*64, 64, 64, TileType.grass);
        }
    }
}
public void Draw(){
    for(int i=0; i<map.length;i++){
        for(int j=0; i<map[i].length;j++){
            Tile t = map[i][j];
            DrawQuadTex(t.getTexture(), t.getX(), t.getY(), t.getWidth(), t.getHeight());

        }
    }
}

My code is in multiple classes, which I will paste as well if needed

2 Answers2

0

Your loop is broken. You incorrectly have i < map[i].length, the i here should be a j!

for(int i=0; i<map.length;i++){
    for(int j=0; j < map[i].length; j++){
DominicEU
  • 3,585
  • 2
  • 21
  • 32
0

Try to replace i<map[i] with j<map[i] like this:

  for(int j=0; j<map[i].length;j++){

Not:

  for(int j=0; i<map[i].length;j++){

You used the wrong index.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36