Slick2D getGraphics()返回null,发生空指针异常

时间:2014-09-03 03:54:22

标签: java nullpointerexception slick2d

我正在努力让我的slick2d游戏进行渲染,但是container.getGraphics()返回null并且我得到一个空指针异常。我检查了使用:

返回null的getGraphics()
if (container.getGraphics()==null) return;

这修复了npe,但当然我无法渲染任何东西。

堆栈跟踪:

Tue Sep 02 21:42:19 MDT 2014 INFO:Slick Build #237
Exception in thread "Thread-1" java.lang.NullPointerException
    at com.nonuniversal.client.MenuState.render(MenuState.java:17)
    at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
    at com.nonuniversal.client.Game.run(Game.java:33)
    at java.lang.Thread.run(Unknown Source)

请注意LunatopiaClient在运行时需要一个用户名参数...

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;

public class LunatopiaClient{
    private String username;
    AppGameContainer container;
    Game game;

    public LunatopiaClient(String username) {

        this.username = username;

    }
    public static void main(String args[]) {

        if (args.length!=1) {
            System.out.println("Usage: java -jar <filename.jar> <username>");
            return;
        } else {
            LunatopiaClient lunatopiaClient = new LunatopiaClient(args[0]);
            lunatopiaClient.init();
        }

    }
    public void init() {

        try {
            game = new Game("");
            container = new AppGameContainer(game);
            container.setFullscreen(true);
            game.init(container);
            game.init();
        } catch (SlickException e) {
            e.printStackTrace();
        }

    }
    public Game getGame() {
        return game;
    }
    public String getUsername() {
        return username;
    }
}

类游戏

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Game extends StateBasedGame implements Runnable{
    public static final int menuState = 0;
    public static final int gameState = 1;
    private boolean running;
    private AppGameContainer container;
    Thread game;

    public Game(String name) {
        super(name);
    }
    public void initStatesList(GameContainer container) throws SlickException {
        this.container = (AppGameContainer) container;
        addState(new MenuState());
        addState(new GameState());
    }
    public void init() throws SlickException {
        enterState(menuState);
        game = new Thread(this);
        game.start();
    }
    public void run() {
        running = true;
        while (running) {
            try {
                update(container, 0);
                render(container, container.getGraphics());
            } catch (SlickException e) {
                e.printStackTrace();
            }

        }
    }
}

类MenuState

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class MenuState extends BasicGameState{

    public void init(GameContainer container, StateBasedGame game)
            throws SlickException {

    }
    public void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException {
        g.drawString("Hello World", 0, 0);
    }
    public void update(GameContainer container, StateBasedGame game, int arg)
            throws SlickException {

    }
    public int getID() {
        return 0;
    }

}

编辑:拼写

1 个答案:

答案 0 :(得分:0)

不幸的是,您在非常非正统的庄园中配置了很多代码。首先,Slick2d会自动为程序的执行提供线程,因此您不需要自己编写线程。

您似乎也在调用不存在的方法,例如,游戏中未定义init(容器)。此外,您将容器存储为不需要的数据值,因此您将获得图形上下文的NPE。

如果您刚刚开始使用Slick2d,我建议您查看有关如何开始使用的newsboston的视频教程。否则不幸的是,我相信您可能必须重新启动项目,因为它在当前状态下无法使用。希望这会有所帮助:)