删除文件:空指针异常
我正在使用这个逻辑来删除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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 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().
递归删除文件/文件夹:
to delete files/folders recursively:
这个:
if(fileOrDirectory.isDirectory())
应该是if(fileOrDirectory != null && fileOrDirectory.isDirectory())
和filesOrDirectory.delete( );
应该是this:
if(fileOrDirectory.isDirectory())
should beif(fileOrDirectory != null && fileOrDirectory.isDirectory())
andfilesOrDirectory.delete();
should beApache 的 commons-io FileUtils
Apache's commons-io FileUtils