gpt4 book ai didi

java - ArrayIndexOutOfBoundsException,同时未访问任何索引

转载 作者:行者123 更新时间:2023-12-02 07:03:27 30 4
gpt4 key购买 nike

由于某种原因,我收到 ArrayIndexOutOfBoundsException 错误,我没有尝试访问数组的任何元素,我想做的就是设置大小,并通过引用传递 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();
}
}
}

错误:

    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().

根据 getRGB(...) 的 javadoc方法:

"An ArrayOutOfBoundsException may be thrown if the region is not in bounds. However, explicit bounds checking is not guaranteed."

<小时/>

至于异常的原因,我认为问题是 pixels 数组不够大,无法容纳您尝试提取的图像区域。 size 和您正在阅读的图像的尺寸之间没有明显的相关性。 (但是,目前尚不清楚您在 load 方法中实际尝试执行的操作...)

关于java - ArrayIndexOutOfBoundsException,同时未访问任何索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369875/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com