0

因此,我尝试使用 Slick2D 加载图像,但由于某种原因,当我尝试将图像绘制到屏幕时出现以下错误:

Sat Sep 28 16:37:59 NZST 2013 ERROR:null
java.lang.NullPointerException
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1350)
    at org.newdawn.slick.Graphics.drawImage(Graphics.java:1399)
    at Game.render(Game.java:58)
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:703)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
    at Game.main(Game.java:42)
Sat Sep 28 16:37:59 NZST 2013 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
    at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:706)
    at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
    at Game.main(Game.java:42)

我正在使用它来加载我的图像:

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Textures {

    static Image grass;
    Game game;


    public Textures() {
            try {
                grass = new Image("grass.jpg");
            } catch (SlickException e) {
                e.printStackTrace();
            }
    }

}

我正在使用它来绘制我的图像:

for(int x = 0; x < app.getWidth(); x+=32) {
    for(int y = 0; y < app.getHeight(); y+=32) {
        g.drawImage(textures.grass, x, y);
    }
}
4

1 回答 1

1

如果您NullPointerException在尝试绘制 时得到Image,通常是因为图像加载器在指定位置找不到图像。如果您正在使用Eclipse,请务必将您的图像存储在一个source folder(例如名为“资源”)中。这可确保图像在编译时移动到bin文件夹(可能需要刷新源文件夹)。

无论您的设置如何,请确保图像的路径正确。

于 2013-10-01T14:26:03.943 回答