我目前正在 Netbeans 中使用 MIDlet(我正在使用 Visual MIDlet),并且正在抛出 NullPointerException 但我不知道为什么。
注意:程序在模拟器上运行时不会抛出异常,只有在按下 OK 命令按钮时才会抛出异常。
这是我得到的错误
TRACE: <at java.lang.NullPointerException: 0>, Exception caught in Display class
java.lang.NullPointerException: 0
at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=46
at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=74
at com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(), bci=37
at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=36
at com.sun.midp.chameleon.CWindow.keyInput(), bci=38
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=17
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:619)
我已经删除了所有与异常无关的代码,以便您更容易阅读。如果我有代码,下面是一个简化版本,它会引发上述异常。
package stMidlet;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class StoryMidlet extends MIDlet implements CommandListener {
private boolean midletPaused = false;
private Command commandOk1;
private Form form1;
private TextField textField1;
public StoryMidlet() {
commandOk1 = new Command("Ok", Command.OK, 1);
textField1 = new TextField("Enter value: ", null, 120, TextField.ANY);
form1 = new Form(null, new Item[]{textField1});
form1.addCommand(commandOk1);
Display.getDisplay(this).setCurrent(form1);
}
/* There were some methods here pre-inserted by netbeans. */
/* I have not changed these, but I can post them if you need me too */
/* initialize() */
/* startMIDlet() */
/* resumeMidlet() */
/* switchDisplayable */
/* getDisplay() */
/* exitMidlet() */
/* startApp() */
/* pauseApp() */
/* destroyApp() */
public void commandAction(Command c, Displayable d) {
if (c == commandOk1)
{
System.out.println("Test");
}
}
}
我一直试图解决这个问题至少一个小时,但没有成功。我能想到的唯一值得一提的是:
- Netbeans 使用Display.getDisplay(this).....行显示警告,表示构造函数中存在泄漏。我将它移到了初始化()方法中,该方法消除了警告,但异常仍然发生。
任何帮助将不胜感激。
谢谢,汤姆。