Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Index -25 out of bounds for length 400

1 resposta
javajavascriptprogramação
A
public World(String path) {

try {

BufferedImage map = ImageIO.read(getClass().getResource(path));

int[] pixels = new int[map.getWidth() * map.getWidth()];

WIDTH = map.getWidth();

HEIGHT = map.getHeight();

tiles = new Tile [map.getWidth() * map.getWidth()];

map.getRGB(0, 0, map.getWidth(), map.getHeight(), pixels, 0, map.getWidth());

for(int xx = 0; xx < map.getWidth(); xx++) {

for(int yy = 0; yy < map.getHeight(); yy++)	{

int pixelAtual = pixels[xx +(yy<em>map.getWidth())];

tiles[xx + (yy</em>WIDTH)] = new FloorTile(xx<em>16,yy</em>16,Tile.TYLE_FLOOR);	

if(pixelAtual == 0xFF000000) {

//Floor/Chão

tiles[xx + (yy<em>WIDTH)] = new FloorTile(xx</em>16,yy<em>16,Tile.TYLE_FLOOR);

} else if(pixelAtual == 0xFFFFFFFF) {

//Wall/Parede	

tiles[xx + (yy</em>WIDTH)] = new FloorTile(xx<em>16,yy</em>16,Tile.TYLE_WALL);

} else if(pixelAtual == 0xFF005DFF) {

//Player/Jogador	

Game.player.setX(xx<em>16);

Game.player.setY(yy</em>16);

tiles[xx + (yy<em>WIDTH)] = new FloorTile(xx</em>16,yy<em>16,Tile.TYLE_FLOOR);	

} else if(pixelAtual == 0xFFFF0000) {

//enemy/Imimigo	

Game.entities.add(new Enemy(xx</em>16,yy<em>16,16,16,Entity.ENEMY_EN));

} else if(pixelAtual == 0xFFFFD800) {

//Bullets/Munições	

Game.entities.add(new Bullets(xx</em>16,yy<em>16,16,16,Entity.BULLET_EN));	

} else if(pixelAtual == 0xFF7FFFFF) {

//Lifepack/vida	

Game.entities.add(new Lifepack(xx</em>16,yy<em>16,16,16,Entity.LIFEPACK_EN));	

} else if(pixelAtual == 0xFF80006E) {

//Weapon/Arma	

Game.entities.add(new Weapon(xx</em>16,yy*16,16,16,Entity.WEAPON_EN));	

}

}
}
} catch (IOException e) {
	e.printStackTrace();
}	
}

public void render(Graphics g) {
int xstart = Camera.x/16;
int ystart = Camera.y/16;

int xfinal = xstart + (Game.WIDTH/16);
int yfinal = ystart + (Game.HEIGHT/16);

for(int xx = xstart; xx <= xfinal; xx++) {
for(int yy = ystart; yy <= yfinal; yy++) {
Tile tile =	tiles[xx + (yy*WIDTH)];
tile.render(g);
}
}
}

por algum motivo sem explicação ta dando erro.

at hello_boy/com.gamesfudido.world.World.render(World.java:73)

at hello_boy/com.gamesfudido.main.Game.render(Game.java:93)

at hello_boy/com.gamesfudido.main.Game.run(Game.java:116)

at java.base/java.lang.Thread.run(Thread.java:1583)

1 Resposta

D

Se não houvesse explicação não haveria solução. Como a computação, por incrível que pareça, é uma ciência exata, apesar de parecer algo um pouco exotérico algumas vezes para os não iniciados, sempre tem um motivo e o motivo do seu erro é bem fácil de entender:

java.lang.ArrayIndexOutOfBoundsException

Que significa: índice do array fora dos limites

Quais os índices válidos para um array de 10 elementos? 0 a 9, certo? Qualquer tentativa de acessar um elemento usando um índice diferente disso vai disparar essa exceção. No seu caso:

Index -25 out of bounds for length 400

Ou seja, o índice -25 (que não faz sentido em Java, mas faz em Python) é um índice inválido em um array de 400 elementos, onde o primeiro índice válido é o 0 e o último é o 399.

Como você é novo aqui, edite sua postagem formatando apropriadamente seu código. Não é tão difícil.

Criado 2 de janeiro de 2024
Ultima resposta 2 de jan. de 2024
Respostas 1
Participantes 2