For some reason I am getting the ArrayIndexOutOfBoundsException error, I am not trying to access any of the elements of the array, all I want to do is set the size, and pass by reference to the i.getRGB().
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Logic;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author Steven, even(RageZone), Zingzags(PokeCommunity)
*/
public class SpriteSheet {
private String path;
private final int size;
private int[] pixels;
public static SpriteSheet tiles = new SpriteSheet("/Tilesets/Outside.png", 256);
public SpriteSheet(String path, int size){
this.path = path;
this.size = size;
pixels = new int[this.size * this.size];
load();
}
public int getPixels(int params){
return pixels[params];
}
public int getSize(){
return size;
}
public int[] getPixels(){
return pixels;
}
private void load(){
try{
BufferedImage im = ImageIO.read(SpriteSheet.class.getResource(path));
int w = im.getWidth();
int h = im.getHeight();
im.getRGB(0, 0, w, h, pixels, 0, w);
} catch(IOException ex){
ex.printStackTrace();
}
}
}
Error:
Exception in thread "Display" java.lang.ExceptionInInitializerError
at Logic.Sprite.<clinit>(Sprite.java:16)
at Logic.Screen.render(Screen.java:46)
at game.Game.render(Game.java:82)
at game.Game.run(Game.java:109)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 65536
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:958)
at Logic.SpriteSheet.load(SpriteSheet.java:47)
at Logic.SpriteSheet.<init>(SpriteSheet.java:27)
at Logic.SpriteSheet.<clinit>(SpriteSheet.java:21)
... 5 more
For some reason I am getting the ArrayIndexOutOfBoundsException error, I am not trying to access any of the elements of the array, all I want to do is set the size, and pass by reference to the i.getRGB().
According to the javadoc for the getRGB(...)
method:
"An ArrayOutOfBoundsException may be thrown if the region is not in bounds. However, explicit bounds checking is not guaranteed."
As to the cause of the exception, I think that the problem is that the pixels
array isn't big enough to hold the region of the image you are trying to extract to it. There's no obvious correlation between size
and the dimensions of the image you are reading. (But then, it is not clear what you are actually trying to do in the load
method ...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.