gpt4 book ai didi

java - 在 Vector 中插入元素时出现错误 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:02:54 27 4
gpt4 key购买 nike

当我尝试运行以下程序时,它会抛出java.lang.NullPointerException。我不明白,为什么当我将元素插入 vector 时会出现此错误。请帮我解决以下问题。提前致谢。

    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
}

}
}

最佳答案

您从未实例化过Vector。在错误行之前的某个位置,您必须执行以下操作:

mGameCanvasList = new Vector();

关于java - 在 Vector 中插入元素时出现错误 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22533048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com