I would love to receive some help from second eyes, for I have struggled to identify what is wrong with my code.
In the Eclipse IDE I am attempting to implement a Google Maps activity that reads seaship MMSI codes from a file in the assets folder (/assets/mmsi.txt) and adds maps markers to their respective locations. I am able to read from this file using the BufferedReader class but am having problems in converting the resulting String lines to double using the Double.parseDouble function.
LogCat detects the following problems.
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
In other words, there seems to be a NullPointerException in the following code:
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();
}
}
But, while debugging the variable line is infact not null! (Sorry not enough REP for images)
Debugging Screenshot
Another SO user had quite a similar problem, but his variables did seem to be badly initialized.
parseDouble throwing Null Pointer in my code [closed]
Double.parseDoubleisn’t the problem. Your assignment is:You never assign a non-null value to
mmsi, hence the error when you try to write into it. If you want further proof of this, split your code into:You’ll see that the exception occurs on the second line, not the first.
Given that you don’t know how many lines you’ll need, I’d suggest you use an
ArrayListinstead:EDIT: Additionally, you should be closing your file in a
finallyblock. Currently, if an exception is thrown while you’re reading, you leave the file open. And your currentcatchblock implementations aren’t as useful as just letting the exception be thrown, IMO.