我很想得到第二眼的帮助,因为我一直在努力找出我的代码出了什么问题。
在 Eclipse IDE 中,我试图实现一个 Google Maps 活动,该活动从 assets 文件夹 ( /assets/mmsi.txt ) 中的文件中读取 seaship MMSI 代码,并将地图标记添加到它们各自的位置。我可以使用该类从该文件中读取,但在使用该函数将结果行BufferedReader
转换为双精度时遇到问题。String
Double.parseDouble
LogCat 检测到以下问题。
01-23 17:59:11.672: E/AndroidRuntime(18242): FATAL EXCEPTION: main
01-23 17:59:11.672: E/AndroidRuntime(18242): java.lang.RuntimeException: Unable to resume activity {com.aquamet.saramap/com.aquamet.saramap.MainActivity}: java.lang.NullPointerException
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.os.Looper.loop(Looper.java:123)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-23 17:59:11.672: E/AndroidRuntime(18242): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 17:59:11.672: E/AndroidRuntime(18242): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 17:59:11.672: E/AndroidRuntime(18242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
01-23 17:59:11.672: E/AndroidRuntime(18242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
01-23 17:59:11.672: E/AndroidRuntime(18242): at dalvik.system.NativeStart.main(Native Method)
01-23 17:59:11.672: E/AndroidRuntime(18242): Caused by: java.lang.NullPointerException
01-23 17:59:11.672: E/AndroidRuntime(18242): at com.aquamet.saramap.MainActivity.getMMSI(MainActivity.java:173)
01-23 17:59:11.672: E/AndroidRuntime(18242): at com.aquamet.saramap.MainActivity.setupRegion(MainActivity.java:136)
01-23 17:59:11.672: E/AndroidRuntime(18242): at com.aquamet.saramap.MainActivity.onResume(MainActivity.java:82)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.Activity.performResume(Activity.java:3827)
01-23 17:59:11.672: E/AndroidRuntime(18242): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
01-23 17:59:11.672: E/AndroidRuntime(18242): ... 12 more
换句话说,以下代码中似乎存在 NullPointerException:
private double[] getMMSI() throws IOException {
AssetManager am = this.getAssets();
double[] mmsi = null;
BufferedReader br = null;
try {
InputStream file_stream = am.open("mmsi.txt");
br = new BufferedReader(new InputStreamReader(file_stream));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line;
for (int i=0; (line = br.readLine()) != null; i++) {
// Following line seems to be the problem (line 173).
mmsi[i] = Double.parseDouble(line);
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
但是,在调试变量时line
实际上不为空!(抱歉没有足够的 REP 用于图像)
调试屏幕截图
另一个 SO 用户也有类似的问题,但他的变量似乎没有正确初始化。 parseDouble 在我的代码中抛出空指针 [关闭]