I am building an Android application and I have chosen to use flat flies in external storage for my persistence method. I have a class that extends Application with some static instance methods. After debugging a few times, I note that none of the instance variables are null, and the NullPointerException is coming from within Context.getExternalFilesDir(null).
I would like to preface any answers with it is not returning null. Also, I have the correct permissions in my manifest, as well as a check for if the SD card is mounted to the device.
This is the class that holds the call to getExternalFilesDir(null) and throws the error.
public class WhileImOut extends Application {
public static TaskManager taskManager;
private static Context appContext;
public WhileImOut() {
super();
appContext = this;
initialize();
}
public static void initialize() {
if (taskManager == null && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try{
File f = appContext.getExternalFilesDir(null); // Exception being thrown here
taskManager = new TaskManager(f.getAbsolutePath());
}catch(NullPointerException e){
Log.d("Bad","NPE");
}catch(Exception e){
}
}
}
}
This is in my AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Any help would be greatly appreciated. I’ve been struggling with this for a few hours, and I will take any advice that is given. Thank you
EDIT: LogCat information
10-10 18:55:56.424: E/AndroidRuntime(964): FATAL EXCEPTION: main
10-10 18:55:56.424: E/AndroidRuntime(964): java.lang.RuntimeException: Unable to resume activity {zsmith.capstone.whileimout/zsmith.capstone.whileimout.TaskListActivity}: java.lang.NullPointerException
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.os.Looper.loop(Looper.java:137)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-10 18:55:56.424: E/AndroidRuntime(964): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 18:55:56.424: E/AndroidRuntime(964): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 18:55:56.424: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-10 18:55:56.424: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 18:55:56.424: E/AndroidRuntime(964): at dalvik.system.NativeStart.main(Native Method)
10-10 18:55:56.424: E/AndroidRuntime(964): Caused by: java.lang.NullPointerException
10-10 18:55:56.424: E/AndroidRuntime(964): at zsmith.capstone.whileimout.TaskListActivity.onResume(TaskListActivity.java:58)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.Activity.performResume(Activity.java:5082)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
You are doing your initialization in the constructor; this will not work. You should be doing your initialization in
onCreate(). When the constructor is invoked, the context has not been properly initialized.(By the way, is there any reason to have a separate
initialize()method? If so, is there any reason for it to bepublic?)