m4n07
m4n07

Reputation: 2287

Crash on Calling getFilesDir()

Crash on calling getFilesDir() , Here is the code

public void generateFile() throws IOException{


    File fileDir = getFilesDir(); //crashing

    String filedir=fileDir.toString();

    String OUTPUT_FILE = "123.txt";
    String data = "TEST DATA to WRITE";

    File newFile = new File(fileDir,OUTPUT_FILE);

    ........

and stacktrace

03-24 16:33:33.623: VERBOSE/APP(2180): Calling GenerateFile
03-24 16:33:33.623: DEBUG/AndroidRuntime(2180): Shutting down VM
03-24 16:33:33.623: WARN/dalvikvm(2180): threadid=1: thread exiting with uncaught exception (group=0x4001d8a8)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180): FATAL EXCEPTION: main
03-24 16:33:33.670: ERROR/AndroidRuntime(2180): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Android.APP/com.Android.APP.Readings}: java.lang.NullPointerException
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.view.View.performClick(View.java:2408)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.view.View$PerformClick.run(View.java:8818)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.os.Handler.handleCallback(Handler.java:587)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.os.Looper.loop(Looper.java:123)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at java.lang.reflect.Method.invoke(Method.java:521)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at dalvik.system.NativeStart.main(Native Method)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180): Caused by: java.lang.NullPointerException
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.Android.APP.Logging.generateFile(Logging.java:33)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.Android.APP.Readings.onCreate(Readings.java:90)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     ... 18 more

Upvotes: 3

Views: 2750

Answers (2)

untill
untill

Reputation: 1593

The OP didn't provide sufficient information in order to be able to suggest a solution, but I had a similar problem along the same lines, upon which I moved the call to the method including getFilesDir(). Before it was called when defining a MyActivity field, and after I moved it into that activity's onCreate(), the exception was gone.

Upvotes: 1

Blundell
Blundell

Reputation: 76536

Does your class extend Activity? Need a bit more context to the code for a proper answer.

EDIT

Where are you calling generateFile() ? You may be asking for the FilesDirectory before your app is fully loaded.

You just need to do some checking:

public void generateFile() throws IOException{

 File fileDir = getFilesDir(); //crashing
 if(fileDir != null){
   String filedir=fileDir.toString();

   String OUTPUT_FILE = "123.txt";
   String data = "TEST DATA to WRITE";

   File newFile = new File(fileDir,OUTPUT_FILE);
 } else {
    Log.e("MyApp", "Something went wrong, filesDir is null");
 }
}

Upvotes: 2

Related Questions