You never instantiated the Vector
. Somewhere before the error line, you must do this:
mGameCanvasList = new Vector();
Pergunta
When I try to run the following program, it throws a
java.lang.NullPointerException
. I do not understand, why I am getting this error when I insert an element into a vector. Please help me resolve the following problem. Thanks in advance.
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
}
}
}
Solução
You never instantiated the Vector
. Somewhere before the error line, you must do this:
mGameCanvasList = new Vector();