当我尝试运行以下程序时,它会抛出一个
java.lang.NullPointerException
。我不明白,为什么我在向量中插入元素时会出现此错误。请帮我解决以下问题。提前谢谢。
public class GameMidlet extends MIDlet implements CommandListener {
GameCanvas game;
Display display;
Command exitCommand;
Command restartCommand;
Vector mGameCanvasList;
public void startApp() {
display = Display.getDisplay(this);
game = new GameCanvas();
//i am getting error at here when game object inset in vector
mGameCanvasList.addElement(game);
GameCanvas fistList = (GameCanvas) mGameCanvasList.elementAt(0);
display.setCurrent(fistList);
exitCommand = new Command("Exit", Command.EXIT, 0);
restartCommand = new Command("Restart", Command.OK, 0);
fistList.addCommand(exitCommand);
fistList.addCommand(restartCommand);
fistList.setCommandListener(this);
fistList.setCommandListener(this);
fistList.startThread();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(c == exitCommand){
// System.out.println("Exit");
destroyApp(true);
notifyDestroyed();
}
if(c == restartCommand){
// game
}
}
}
答案 0 :(得分:2)
您从未实例化Vector
。在错误行之前的某个地方,你必须这样做:
mGameCanvasList = new Vector();