Question

I'm attempting to copy a file programatically using:

public void function1(int id) {
    Toast.makeText(this, "Copy", Toast.LENGTH_SHORT).show();

    try {
        copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


public static void copyDirectoryOneLocationToAnotherLocation(
        File sourceLocation, File targetLocation) throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < sourceLocation.listFiles().length; i++) {

            copyDirectoryOneLocationToAnotherLocation(new File(
                    sourceLocation, children[i]), new File(targetLocation,
                    children[i]));
        }
    } else {

        InputStream in = new FileInputStream(sourceLocation);

        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

}

However I'm getting an error stating:

FATAL EXCEPTION: main NullPointerException at com.example.project.FileChooser.function1(FileChooser.java:164)

on the line:

copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);

I think I may have used incorrect paramaters passed in copyDirectoryOneLocationToAnotherLocation

I thought perhaps I should have used: sourceLocation, targetLocation but that did not work for me either.

Logcat:

01-09 15:54:49.026: E/AndroidRuntime(15378): FATAL EXCEPTION: main
01-09 15:54:49.026: E/AndroidRuntime(15378): java.lang.NullPointerException
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.copyDirectoryOneLocationToAnotherLocation(FileChooser.java:205)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.copyDirectoryOneLocationToAnotherLocation(FileChooser.java:207)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.function1(FileChooser.java:164)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.example.project.FileChooser.onContextItemSelected(FileChooser.java:148)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.app.Activity.onMenuItemSelected(Activity.java:2597)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3663)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:963)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.widget.AbsListView$1.run(AbsListView.java:3571)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.os.Handler.handleCallback(Handler.java:725)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.os.Looper.loop(Looper.java:153)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at android.app.ActivityThread.main(ActivityThread.java:5297)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at java.lang.reflect.Method.invoke(Method.java:511)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-09 15:54:49.026: E/AndroidRuntime(15378):    at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

If you're getting a NullPointerException on this line:

copyDirectoryOneLocationToAnotherLocation(currentDir, currentDir);

That means that currentDir is null. I don't see you assign it to anything in your code, so assign it to the Directory and then it will not be null anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top