尝试读取文件时出现 java.lang.NullPointerException
你好 我这里有一个小程序,它从命令行获取一个目录,并添加该目录及其子文件夹中的所有文件..所以这是代码:
package oop.ex1.filescript;
import java.io.*;
import java.util.ArrayList;
public class MyFileScript {
public static void main(String[] args) {
File dir = new File(args[0]);
//LINE 13
ArrayList<File> files = readAllFiles(dir.listFiles());
}
private static ArrayList<File> readAllFiles(File[] dir){
ArrayList<File> result = new ArrayList<File>();
//LINE 28
for(File itr : dir) {
result.add(itr);
if (!itr.isFile()) {
File[] temp = itr.listFiles();
//LINE 32
ArrayList<File> deeperList = readAllFiles(temp);
result.addAll(deeperList);
}
}
return result;
}
}
现在,当我在某些文件夹上运行它时,其中的文件不多没关系,但是当我向它提供包含大量文件的文件夹时,我得到:
Exception in thread "main" java.lang.NullPointerException
at oop.ex1.filescript.MyFileScript.readAllFiles(MyFileScript.java:28)
at oop.ex1.filescript.MyFileScript.readAllFiles(MyFileScript.java:32)
at oop.ex1.filescript.MyFileScript.readAllFiles(MyFileScript.java:32)
at oop.ex1.filescript.MyFileScript.main(MyFileScript.java:13)
知道为什么会发生这种情况吗?谢谢! PS我标记了行号!
hi
i have a little program here that gets a directory from the command line and adds all the files in this directory and its sub-folders.. so here's the code:
package oop.ex1.filescript;
import java.io.*;
import java.util.ArrayList;
public class MyFileScript {
public static void main(String[] args) {
File dir = new File(args[0]);
//LINE 13
ArrayList<File> files = readAllFiles(dir.listFiles());
}
private static ArrayList<File> readAllFiles(File[] dir){
ArrayList<File> result = new ArrayList<File>();
//LINE 28
for(File itr : dir) {
result.add(itr);
if (!itr.isFile()) {
File[] temp = itr.listFiles();
//LINE 32
ArrayList<File> deeperList = readAllFiles(temp);
result.addAll(deeperList);
}
}
return result;
}
}
now when i run it on certain folders with not many files in them everything is okay, but when i feed it folders with a lot of files i get:
Exception in thread "main" java.lang.NullPointerException
at oop.ex1.filescript.MyFileScript.readAllFiles(MyFileScript.java:28)
at oop.ex1.filescript.MyFileScript.readAllFiles(MyFileScript.java:32)
at oop.ex1.filescript.MyFileScript.readAllFiles(MyFileScript.java:32)
at oop.ex1.filescript.MyFileScript.main(MyFileScript.java:13)
any idea as for why it happens? thanks!
p.s. i marked line numbers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看起来 dir.listFiles 返回 null。
引用文档,“如果此抽象路径名不表示目录,或者发生 I/O 错误,则返回 null。”。
Looks like dir.listFiles returned null.
Quoting the doc, "Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.".
第 28 行中的 dir 变量可能为 null。您可能希望在调用
readAllFiles
之前检查dir.listFiles()
的结果。The
dir
variable in line 28 is probably null. You might want to include a check on the result ofdir.listFiles()
before callingreadAllFiles
.而不是
使用
如果它不是文件,则不一定是目录。
Instead of
use
If it is not a file it is not neccesarily a directory.
我会这样写。我测试时效果很好:
I'd write it this way. Works fine when I test it:
您应该执行
if (itr.isDirectory())
并检查 nullif (tmp != null)
。You should do
if (itr.isDirectory())
and check for nullif (tmp != null)
.我将使用
listFile(FileFilter)
并让您的FileFilter
返回File.canRead()
。查看是否获得了所有预期的文件,如果没有,请找出它们不可读的原因。I would use a
listFile(FileFilter)
and have yourFileFilter
returnFile.canRead()
. See if you get all expected files and if not figure out why they aren't readable.