J2ME (Java) - 显示类中捕获空指针异常
我目前正在 Netbeans 中使用 MIDlet(我正在使用 Visual MIDlet),并且抛出 NullPointerException,但我不知道为什么。
注意:程序在模拟器上运行时不会抛出异常,只有按下“确定命令”按钮时才会抛出异常。
这是我得到的错误,
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)..... 行,表示构造函数中存在泄漏。我将它移到了initialize()方法中,该方法消除了警告,但异常仍然发生。
任何帮助将不胜感激。
谢谢,汤姆。
I'm currently working with MIDlets (I am using a Visual MIDlet) in Netbeans, and a NullPointerException is being thrown but I do not know why.
Note: The exception is not thrown when the program runs on the emulator, only when the OK Command button is pressed.
Here is the error I get
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)
I have stripped out all of the code unrelated to the exception, so that you can read it easier.
Below is a simplified version if the code I have, which throws the above exception.
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");
}
}
}
I have been trying to solve this for at least an hour, with no prevail.
The only thing I can think of worth mentioning is:
- Netbeans showed a warning with the line Display.getDisplay(this)..... saying there was a leak in the constructor. I moved it into the initialize() method which sedated the warning, but the exception still occurs.
Any help will be greatly appreciated.
Thanks, Tom.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:我可能需要修改我的答案,因为我认为我所说的不相关,但我会保留它,因为它可能会有所帮助!
我已经很长时间没有使用 J2ME 了,但是在查看了一些旧代码之后,我发现我从未在构造函数中做过任何有用的事情。我敢打赌,您对 Display.getDisplay(this) 的调用会导致 NullPtrException,因为某些内容尚未初始化。事实上,我非常确定在构造函数中使用 this 指针肯定会导致此类错误。
尝试在 startApp() 函数中处理 Display,如果我引用的这段代码是正确的,您应该保留一个布尔值来标记您的 MIDlet 是否已初始化。
您可以在这里查看我的一些旧代码以供参考:
http://code.google.com/p/jmingle/source/browse/trunk/src/org/oep/jmingle/JMingle.java#68
EDIT: I might need to redact my answer because I think what I said is not relevant, but I'll leave it up on the off-chance that it could help!
It's been a long time since I have worked in J2ME, but having looked at some old code I noticed I never did anything that useful in the constructor. I'm betting your call to Display.getDisplay(this) is causing a NullPtrException because something hasn't been initialized yet. In fact, I'm pretty sure using the this pointer in a constructor is fairly certain to cause this type of error.
Try dealing with Display in the startApp() function, and if this code I'm referring to is correct, you should keep a boolean that marks if your MIDlet has been initialized yet or not.
You can look at some old code of mine here for reference:
http://code.google.com/p/jmingle/source/browse/trunk/src/org/oep/jmingle/JMingle.java#68
也许你需要添加
Maybe you need to add
我也注意到了......
缺乏
并且总是得到空指针异常......这很有效,但似乎当我们的代码变得太复杂时我们经常忘记它^^
I also noticed it....
Lacked the
and always got the null-pointer exception... This works well but it seems that we forget it too often when our code gets too complex ^^
您必须按顺序执行:
MIDlet.getDisplay.setCurrent(form1);
如果您在添加命令和侦听器之前调用“setCurrent”, form1 屏幕仍然出现,但是当您点击命令时,它会引发上述错误。
You must do in order:
MIDlet.getDisplay.setCurrent(form1);
If you invoke 'setCurrent' before adding the commands and listener, the form1 screen still appear but when you hit a command, it raises above error.