我正在尝试从资源文件夹中读取图像 这是遗传的文件夹
/src
-/classes
--Sprite.java
--Main.java
--/chars
---George.java
-/res
--george.png
Sprite.java(用于读取精灵表)
public class Sprite {
private static BufferedImage spriteSheet;
private static final int TILE_SIZE = 32;
public static BufferedImage loadSprite(URL path) {
Image sprite = null;
sprite = Toolkit.getDefaultToolkit().getImage(path);
return ((ToolkitImage)sprite).getBufferedImage();
}
public static BufferedImage getSprite(URL path, int xGrid, int yGrid) {
spriteSheet = loadSprite(path);
return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
George.java
public class George {
public George() {
URL path = getClass().getResource("/res/george.png");
BufferedImage Stand = Sprite.getSprite(path, 2, 1);
}
}
我在Exception in thread "main" java.lang.NullPointerException
和return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE);
BufferedImage Stand = Sprite.getSprite(path, 2, 1);
我似乎无法正常工作,请在这里帮助我。