Java 单例空指针异常

标签 java singleton nullpointerexception

我正在从头开始用 Java 构建我的第一个游戏。我决定集中游戏主要操作(处理输入等)的 GameWorld 类最好作为单例实现(使用枚举)。枚举的相关代码如下。

public enum GameWorld {
    INSTANCE;
    private static InputController input = InputController.getInput();
    public EntityPlayer player = new EntityPlayer(10, 10, 5, 5);

    public static GameWorld getWorld() {
        return INSTANCE;
    }

    public InputController getInputController() {
        return input;
    }
}

异常发生在EntityPlayer的构造函数中。代码和堆栈跟踪如下。

public class EntityPlayer implements Entity, InputListener {
    private int xPos;
    private int yPos;
    private int width;
    private int height;

    // Velocity of object
    // Determines where it sets itself on update
    private int xVel;
    private int yVel;
    private GameWorld world;

    private InputController input;

    private boolean solid;

    public EntityPlayer(int x, int y, int width, int height) {
        xPos = x;
        yPos = y;
        this.width = width;
        this.height = height;
        solid = true;
        xVel = 0;
        yVel = 0;
        world = getWorld();
        input = world.getInputController();
        input.registerKeyListener(this);
    }

    @Override
    public Graphics draw(Graphics g) {
        g.setColor(Color.yellow);
        g.fillRect(xPos, yPos - height, width, height);
        return g;
    }

    @Override
    public void update() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getXPos() {
        return xPos;
    }

    @Override
    public int getYPos() {
        return yPos;
    }

    @Override
    public Rectangle getRect() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isSolid() {
        return solid;
    }

    @Override
    public void kill() {

    }

    @Override
    public GameWorld getWorld() {
        return GameWorld.getWorld();
    }

    @Override
    public void sendKeyPress(KeyEvent ke) {
        System.out.println(ke.getKeyChar());
    }

    @Override
    public void sendMouseMove(MouseEvent me) {

    }

}

堆栈跟踪:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.pvminecraft.gameworld.Main.<clinit>(Main.java:14)
Caused by: java.lang.NullPointerException
at com.pvminecraft.gameworld.entities.EntityPlayer.<init>(EntityPlayer.java:45)
at com.pvminecraft.gameworld.GameWorld.<init>(GameWorld.java:15)
at com.pvminecraft.gameworld.GameWorld.<clinit>(GameWorld.java:13)
... 1 more

Main.java 的第 14 行是我获取 GameWorld 的另一个实例进行测试。我不确定为什么这会引发异常。如果我删除 EntityPlayer 中对 GameWorld 的引用,它就会消失。如果您需要 Main.java 的代码,请在评论中告诉我,我将发布它。 谢谢!

编辑: EntityPlayer 中的第 45 行是“input = world.getInputController();”我很确定世界是空的,尽管我不知道为什么。

最佳答案

你正在让自己陷入困境。

您想要初始化 GameWorld.INSTANCE 变量。在此之前,您必须初始化 GameWorld 类的所有字段。初始化所有字段后,将分配变量INSTANCE。在此之前,它仍然具有默认值 null

在初始化期间,字段player被初始化。在该初始化中,您已经访问了 INSTANCE 字段。所以你有一个循环依赖。

您确实必须解耦您的类,以便它们变得更加相互独立。

关于Java 单例空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7424210/

相关文章:

beego 中的 golang 单例

java - 什么是NullPointerException,我该如何解决?

java - 为什么我会收到 NullPointerException?

Android ArrayList 迭代器返回 null

java - 如何在 JSP 中收集用户输入,以便将其存储在数据库中

java - 如何修改一个大的 json 字符串?

java - 渴望实例化单例

c# - 自动属性初始化器单例实现

java - 如何配置 eclipse 构建路径来开发 ant 任务?是否有用于 ant tasks dev 的插件?

java - Threadpoolexecutor 不更新并发 HashMap