简体   繁体   English

J2ME应用程序有问题! (空指针异常)

[英]J2ME Application problem! (Null pointer exception)

I'm creating an application that installs 3 Record Stores for the first run. 我正在创建一个应用程序,该应用程序将为首次运行安装3个唱片商店。 Then on it has to work with the already installed values. 然后,它必须使用已经安装的值。 The application works fine during the first run both in the emulator and in a mobile. 该应用程序在模拟器和移动设备中的首次运行期间均能正常运行。 But the second time run shows a null pointer exception after my splash screen loads. 但是第二次运行在启动屏幕加载后显示了空指针异常。 After the splash screen, I've got the loading of record stores. 在启动屏幕之后,我已经加载了唱片商店。 But the record stores also get deleted, updated during the first run cause of some features. 但是,由于某些功能的首次运行,记录存储也会被删除和更新。 During such times, the midlet runs without any problem. 在这种情况下,midlet可以正常运行。 But when I open the application second time in my mobile, it springs up with an error message saying null pointer exception. 但是,当我第二次在手机中打开应用程序时,它弹出并显示一条错误消息,指出空指针异常。

I need the following help... 1. Can I run the emulator again with the old recorstores? 我需要以下帮助... 1.我可以使用旧的记录库再次运行模拟器吗? If so how? 如果可以,怎么办? 2. How can I rectify the problem of null pointer exception? 2.如何纠正空指针异常问题?

Please help. 请帮忙。

To the point: just read the stacktrace and fix the null pointer accordingly. 至此:只需阅读stacktrace并相应地修复null指针即可。

The first line of the stacktrace should contain a line number of the source code where it is been caused. stacktrace的第一行应包含引起它的源代码的行号。 Open the source code and go to that line. 打开源代码并转到该行。 It should look like: 它应该看起来像:

someObject.doSomething();

Especially look there where the dot operator . 特别要看那里的点运算符. is used to access or invoke some object instance. 用于访问或调用某些对象实例。 A NullPointerException on such a code line means that the someObject is actually null . 这样的代码行上的NullPointerException意味着someObject实际上为null It simply refers nothing . 它只是什么都没有 You can't access it nor invoke any methods on it. 您不能访问它,也不能调用任何方法。

All you need to do to fix a NullPointerException is to ensure that someObject is not null: 修复NullPointerException所需要做的就是确保someObject 不为 null:

if (someObject == null) {
    someObject = new SomeObject();
}
someObject.doSomething();

Or alternatively only do the access/invocation if someObject is not null . 或者,如果someObject不为null则仅执行访问/调用。

if (someObject != null) {
    someObject.doSomething();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM