删除文件:空指针异常

发布于 2024-12-26 12:43:47 字数 3329 浏览 0 评论 0原文

我正在使用这个逻辑来删除SD卡中的文件。它正在删除所有文件,但在文件删除操作期间,我在日志中收到 NullPointer 异常。有帮助吗?

我的代码是:

void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) {
        try {
            for (File child : fileOrDirectory.listFiles()) {
                DeleteRecursive(child);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    fileOrDirectory.delete();
}

这是我在删除操作期间收到的消息:

01-01 01:28:43.440: W/System.err(4183): java.lang.NullPointerException
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:34)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.onCreate(Delete_sdcard1Activity.java:23)
01-01 01:28:43.450: W/System.err(4183):     at android.app.Activity.performCreate(Activity.java:4465)
01-01 01:28:43.450: W/System.err(4183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-01 01:28:43.450: W/System.err(4183):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 01:28:43.450: W/System.err(4183):     at android.os.Looper.loop(Looper.java:137)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-01 01:28:43.450: W/System.err(4183):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 01:28:43.450: W/System.err(4183):     at java.lang.reflect.Method.invoke(Method.java:511)
01-01 01:28:43.450: W/System.err(4183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 01:28:43.450: W/System.err(4183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 01:28:43.450: W/System.err(4183):     at dalvik.system.NativeStart.main(Native Method)

I am using this logic to delete files in sdcard. It is deleting all the files, but I am getting NullPointer exceptions in Logs during the file delete operation. Any Help?

My Code is:

void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) {
        try {
            for (File child : fileOrDirectory.listFiles()) {
                DeleteRecursive(child);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    fileOrDirectory.delete();
}

This is the message I am getting during delete operation:

01-01 01:28:43.440: W/System.err(4183): java.lang.NullPointerException
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:34)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183):     at com.stericsson.sdcard.Delete_sdcard1Activity.onCreate(Delete_sdcard1Activity.java:23)
01-01 01:28:43.450: W/System.err(4183):     at android.app.Activity.performCreate(Activity.java:4465)
01-01 01:28:43.450: W/System.err(4183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-01 01:28:43.450: W/System.err(4183):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 01:28:43.450: W/System.err(4183):     at android.os.Looper.loop(Looper.java:137)
01-01 01:28:43.450: W/System.err(4183):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-01 01:28:43.450: W/System.err(4183):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 01:28:43.450: W/System.err(4183):     at java.lang.reflect.Method.invoke(Method.java:511)
01-01 01:28:43.450: W/System.err(4183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 01:28:43.450: W/System.err(4183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 01:28:43.450: W/System.err(4183):     at dalvik.system.NativeStart.main(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮坠入山谷 2025-01-02 12:43:47

如果 File 对象表示的路径名不是目录或者发生某些 I/O 错误,listFIles() 函数可能返回 null。
作为标准做法,您应该为 listFiles() 的返回值添加 null 检查。

the listFIles() function may return null if the pathname denoted by the File object is NOT a directory or if some I/O error occurs.
As a standard practice, you should add a null check for the return value of listFiles().

愿与i 2025-01-02 12:43:47

递归删除文件/文件夹:

public boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

           return dir.delete();
}

to delete files/folders recursively:

public boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

           return dir.delete();
}
夜无邪 2025-01-02 12:43:47

这个: if(fileOrDirectory.isDirectory()) 应该是 if(fileOrDirectory != null && fileOrDirectory.isDirectory())filesOrDirectory.delete( ); 应该是

   if(filesOrDirectory != null) {
       filesOrDirectory.delete();
   }

this: if(fileOrDirectory.isDirectory()) should be if(fileOrDirectory != null && fileOrDirectory.isDirectory()) and filesOrDirectory.delete(); should be

   if(filesOrDirectory != null) {
       filesOrDirectory.delete();
   }
注定孤独终老 2025-01-02 12:43:47

Apache 的 commons-io FileUtils

FileUtils.deleteDirectory(new File("directory"));

Apache's commons-io FileUtils

FileUtils.deleteDirectory(new File("directory"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文