parseDouble为非null变量抛出NullPointerException

时间:2013-01-23 20:18:28

标签: java android eclipse nullpointerexception


我很乐意从第二只眼睛那里得到一些帮助,因为我一直在努力找出我的代码有什么问题。

在Eclipse IDE中,我尝试实现一个Google Maps活动,该活动从assets文件夹( /assets/mmsi.txt )中的文件中读取seaship MMSI代码,并将地图标记添加到各自的位置。我可以使用BufferedReader类从此文件中读取,但在使用String函数将生成的Double.parseDouble行转换为double时遇到问题。

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时实际上并非null! (抱歉没有足够的REP图像) Debugging Screenshot

另一个SO用户遇到了类似的问题,但他的变量似乎确实被初始化了。 parseDouble throwing Null Pointer in my code [closed]

3 个答案:

答案 0 :(得分:12)

Double.parseDouble不是问题。你的任务是:

double[] mmsi = null;
...
// mssi is still null...
mmsi[i] = Double.parseDouble(line);

您永远不会为mmsi分配非空值,因此当您尝试写入时会出现错误。如果您想进一步证明这一点,请将您的代码拆分为:

double tmp = Double.parseDouble(line);
mmsi[i] = tmp;

您会看到第二行行发生异常,而不是第一行。

鉴于你不知道你需要多少行,我建议你改用ArrayList

List<Double> mmsi = new ArrayList<Double>();
...
mmsi.add(Double.valueOf(line));

编辑:此外,您应该在finally块中关闭文件。目前,如果您在阅读时抛出异常,则将文件保持打开状态。而且你当前的catch块实现并不像仅仅抛出异常那样有用,IMO。

答案 1 :(得分:2)

问题出在:double[] mmsi = null;

您尚未创建阵列。

尝试:

double[] mmsi = new double[10]; // or your desired array size

有关数组的更多信息:Arrays in Java

答案 2 :(得分:1)

双引号mmsi数组从不被初始化以引用除null引用之外的某些东西。因此NPE

 double[] mmsi = null;