在目录上调用 listFiles() 时出现 Java NullPointerException [英] Java NullPointerException when calling listFiles() on a directory

查看:16
本文介绍了在目录上调用 listFiles() 时出现 Java NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用这段代码访问的服务器上有一系列包含书籍的文件夹.我想让这些文件夹中的每一个都成为一个对象,以便稍后我可以对其中的文件进行一些处理.我正在尝试使用此方法将文件夹列表作为 Book 对象返回.

I have a series of folders containing books on a server which I am accessing with this piece of code. I want to make each of these folders an object so I can do some work with the files inside them later on. I'm trying to use this method to return a list of the folders as Book objects.

public List<Book> getBooks(File folder){
    List<Book> books = new ArrayList<Book>();
    for (File f : folder.listFiles()){
        if (f.isDirectory()){
            System.out.println(f.getAbsolutePath() + "" + f.listFiles());
            books.add(new Book(f));
        }
    }
    return books;
}

这个块中的 println 语句应该打印文件夹的直接路径,然后是内存地址以及一些其他信息.但是,当调用 listFiles() 时,它在文件夹中的某处打印出 null.它正在执行此操作的文件夹不为空.然后将这个假设为空的文件夹传递给我的类 init 方法.

The println statement in this block is printing, as it should, the direct path to the folder and then the memory address along with some other information. However, somewhere in the folder it is printing out null when listFiles() is called. The folder that it is doing this on is not empty. This supposedly empty folder is then passed to my class init method.

public Book(File bookFolder) {
    this.bookFolder = bookFolder;
    this.bookPath = bookFolder.getAbsolutePath();

    System.out.println(bookFolder + " " + bookFolder.listFiles());

    for (File f : bookFolder.listFiles()) {
        ...
    }
}

这个块中的 println 语句打印出完全相同的文件夹路径,然后是不同的内存地址,这也是意料之中的.当它到达空"文件夹时,它再次为内存地址打印 null.

The println statement in this block prints out the exact same path to the folder and then a different memory address, which is also expected. When it hits the "empty" folder it prints null for the memory address again.

现在,对于真正的问题,带有 for 循环的那一行是程序崩溃并抛出 NullPointerException 的地方,它甚至在 listFiles 方法的文档中都没有描述.

Now, for the real issue, the line with the for loop is where the program crashes and throws a NullPointerException which isn't even described in the documentation for the listFiles method.

为什么会发生这种情况?另外,为什么我的非空文件夹返回 null?

Why could this be happening? Also, why are my non-empty folders returning null?

推荐答案

listFiles() 方法明确指出它如果此抽象路径名不表示目录,或者如果 I/O 错误发生."

The documentation for the listFiles() method clearly states that it "Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs."

无法列出目录的最常见原因之一是该进程缺乏正确的权限.您是以自己的身份运行它,还是在以不同用户身份运行的某种服务中?

One of the most common reasons that a directory cannot be listed is that the process lacks the right permissions. Are you running this as yourself, or in some sort of service that runs as a different user?

顺便说一下,File API 是一个很好的例子,说明生活可能无一例外.

By the way, the File API is a great example of how bad life can be without exceptions.

这篇关于在目录上调用 listFiles() 时出现 Java NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆