0

I have a method:

private boolean canFileOpen(String fileName) {
    boolean canOpen = true;
    try {
            InputStream test = openFileInput(fileName);
        }
        catch(FileNotFoundException e)
        {
            canOpen = false;
        }
        catch (IOException e)
        {
            canOpen = false;
        }
    return canOpen;
}

whatever I pass into fileName, the app just says "The app has stopped working" and closes. For example I'll pass in "test.txt" and even though it doesn't exist, it shouldn't crash the program. What am I doing wrong here?

EDIT: here is the log:

10-23 17:57:22.564    1476-1476/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cloneeskimo.simplefile/com.cloneeskimo.simplefile.activity_fileeditor}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.app.ContextImpl.makeFilename(ContextImpl.java:2005)
            at android.app.ContextImpl.openFileInput(ContextImpl.java:709)
            at android.content.ContextWrapper.openFileInput(ContextWrapper.java:173)
            at com.cloneeskimo.simplefile.activity_fileeditor.canFileOpen(activity_fileeditor.java:49)
            at com.cloneeskimo.simplefile.activity_fileeditor.initSystems(activity_fileeditor.java:32)
            at com.cloneeskimo.simplefile.activity_fileeditor.onCreate(activity_fileeditor.java:41)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
computingfreak
  • 4,939
  • 1
  • 34
  • 51

2 Answers2

0

fileName is null, based on your stack trace, and based on the now-current implementation of makeFilename() in ContextImpl.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You are catching FileNotFoundException and IOException, and your stack trace clearly says you're getting a NullPointerException.

You aren't catching that exception, so your app crashes.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38