-1

In my android project I want to move a file but it crashes. I used the following code:

copyFile(getResources().openRawResource(R.raw.world_resource_packs), "/storage/emulated/0/games/com.mojang/minecraftWorlds/Escape The Room (Original by Twister Games)/world_resource_packs.json");

public void copyFile(InputStream inputStream, String outputPath){

    System.out.println("1");
    in = inputStream;
    out = null;
    System.out.println("2");
    try {
        out = new FileOutputStream(outputPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println("3");
    byte[] buff = new byte[1024];
    int read = 0;
    System.out.println("4");

    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        System.out.println("5");
        System.out.println("6");
        System.out.println("7");
        System.out.println("8");
        System.out.println("9");
        System.out.println("10");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

And I also put this in my Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

But it still crashes:

E/AndroidRuntime: FATAL EXCEPTION: main Process: twistergames.de.testverschieben, PID: 11228 java.lang.RuntimeException: Unable to start activity ComponentInfo{twistergames.de.testverschieben/twistergames.de.testverschieben.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.write(byte[], int, int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.write(byte[], int, int)' on a null object reference at twistergames.de.testverschieben.MainActivity.copyFile(MainActivity.java:44) at twistergames.de.testverschieben.MainActivity.onCreate(MainActivity.java:23) at android.app.Activity.performCreate(Activity.java:6915) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:156) at android.app.ActivityThread.main(ActivityThread.java:6523) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

Thank you very much!!!

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Shade
  • 1
  • 1

2 Answers2

1

Here is actual error description:

java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.write(byte[], int, int)' on a null object reference at

so you got IOException while tried to open output stream

try {
        out = new FileOutputStream(outputPath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

So there are no file at pointed path.

Also I suggest to don't ignore FileNotFoundException and exit from method or re-throw RuntimeException.

Make sure that file with path "/storage/emulated/0/games/com.mojang/minecraftWorlds/Escape The Room (Original by Twister Games)/world_resource_packs.json" is exist and create it if needed

Andrew
  • 1,383
  • 7
  • 21
0

If you are running Android 6.0+, go to Settings -> Apps -> Your app -> Permissions and grant the permission there. This is a temporary fix for testing. In the long run, you should request runtime permissions.

Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29