Java单例NullPointerException
我正在用Java从头开始构建我的第一个游戏。我已经决定,集中游戏主要操作(处理输入等)的GameWorld类最好作为一个单例实现(使用枚举)。枚举的相关代码如下所示Java单例NullPointerException,java,singleton,nullpointerexception,Java,Singleton,Nullpointerexception,我正在用Java从头开始构建我的第一个游戏。我已经决定,集中游戏主要操作(处理输入等)的GameWorld类最好作为一个单例实现(使用枚举)。枚举的相关代码如下所示 public enum GameWorld { INSTANCE; private static InputController input = InputController.getInput(); public EntityPlayer player = new EntityPlayer(10, 10,
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;
}
}
该异常发生在EntityLayer的构造函数中。下面是代码和堆栈跟踪
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.lang.ExceptionInInitializeError中的异常
在com.pvminecraft.gameworld.Main.(Main.java:14)
原因:java.lang.NullPointerException
位于com.pvminecraft.gameworld.entities.EntityPlayer。(EntityPlayer.java:45)
在com.pvminecraft.gameworld.gameworld.(gameworld.java:15)
在com.pvminecraft.gameworld.gameworld.(gameworld.java:13)
... 还有一个
java的第14行是我得到另一个游戏世界的实例进行测试。我不确定这为什么会引发异常。如果我删除EntityLayer中对游戏世界的引用,它就会消失。如果您需要Main.java的代码,请在注释中告诉我,我会发布它。
谢谢
编辑:
EntityLayer中的第45行是“input=world.getInputController();”我非常确定world为null,尽管我不知道为什么。我没有看到任何保证表达式
InputController.getInput()
返回非null。而且,由于您的代码对调试非常友好(每行至少有一个NullPointerException),所以查看哪个变量为null应该很简单
正如我所说的,我怀疑
input
你在兜圈子
您想初始化GameWorld.INSTANCE
变量。在此之前,您必须初始化GameWorld
类的所有字段。初始化所有字段后,将分配变量实例
。在此之前,它仍然具有默认值null
在初始化过程中,player
字段被初始化。在初始化过程中,您已经访问了实例
字段。所以你有一个循环依赖
你真的必须解耦你的类,使它们彼此更加独立。第45行是什么?哦,这个<代码>输入=world.getInputController()代码>?这不是一个很好的使用
enum
…我从中得到了一个想法:它的评级很高,他引用了一本我听说很好的书。我认为这是正确的。我错了吗?@Oli使用enum作为单例可以说是Java 1.5+的最佳Java单例模式。嗯,在enum中有一个播放器,播放器引用enum——这可能是一个潜在的问题。这是一个世界。我过去在那里有用于调试的println,这告诉我这一点。+1很好。当你发布这篇文章时,我刚刚弄明白了实例
是如何为空的。我还不完全清楚最好的方法是什么。我已经添加了一个setPlayer方法,它现在仍然有效。谢谢