Jerryl15
Jerryl15

Reputation: 548

NullPointerException / IllegalStateException OutputStream.write()

I seem to be having a problem with my method to backup the current database. The method is as follows:

When using a nexus 4, running 4.2.2 the method works fine, but when using an emulator to emulate version API 15, the error occurs. The line which the error occurs on is os.write() thanks.

public void backupDatabase() throws IOException {

    File file = context.getDatabasePath(DATABASE_NAME);
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    File appDir = new File( Environment.getExternalStorageDirectory() + "/AppName/");
    appDir.mkdir();
    File outputDB = new File(appDir, "BackupFile.file");
    //String outputDB = Environment.getExternalStorageDirectory() +"/AppName/BackupFile.file";
    OutputStream os = null;
    try {
        os = new FileOutputStream(outputDB);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // Transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0) {
           os.write(buffer, 0, length); //<---Error.... (Only on emulator)
    }
    // Close the streams
    os.flush();
    os.close();
    fis.close();

    Toast.makeText(context, "Data successfully backed up!", Toast.LENGTH_SHORT).show();
}


01-18 18:57:38.077: E/ACRA(1961): PACKAGE_NAME fatal error : Could not execute method of the activity
01-18 18:57:38.077: E/ACRA(1961): java.lang.IllegalStateException: Could not execute method of the activity
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View$1.onClick(View.java:3044)
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View.performClick(View.java:3511)
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View$PerformClick.run(View.java:14105)
01-18 18:57:38.077: E/ACRA(1961):   at android.os.Handler.handleCallback(Handler.java:605)
01-18 18:57:38.077: E/ACRA(1961):   at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 18:57:38.077: E/ACRA(1961):   at android.os.Looper.loop(Looper.java:137)
01-18 18:57:38.077: E/ACRA(1961):   at android.app.ActivityThread.main(ActivityThread.java:4424)
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invoke(Method.java:511)
01-18 18:57:38.077: E/ACRA(1961):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 18:57:38.077: E/ACRA(1961):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 18:57:38.077: E/ACRA(1961):   at dalvik.system.NativeStart.main(Native Method)
01-18 18:57:38.077: E/ACRA(1961): Caused by: java.lang.reflect.InvocationTargetException
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:57:38.077: E/ACRA(1961):   at java.lang.reflect.Method.invoke(Method.java:511)
01-18 18:57:38.077: E/ACRA(1961):   at android.view.View$1.onClick(View.java:3039)
01-18 18:57:38.077: E/ACRA(1961):   ... 11 more
01-18 18:57:38.077: E/ACRA(1961): Caused by: java.lang.NullPointerException
01-18 18:57:38.077: E/ACRA(1961):   at     PACKAGE_NAME.backupDatabase(DbHelper.java:85)

Upvotes: 0

Views: 775

Answers (1)

Sipka
Sipka

Reputation: 2311

It seems to me that os is null at the moment. If so, then creating the FileOutputStream threw a FileNotFoundException. You should've posted any logs if you had any.

Also be sure to set the permission READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE.

My advice would be to check if os is not null at the moment, and only then write, because the file opening might fail.

Also I wouldn't rely on emulator over an actual device, because it just emulates the system. If you have any real devices you can test on, use them.

Upvotes: 2

Related Questions