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

查看:216
本文介绍了在目录上调用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错误,则返回null。

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?

顺便说一下,文件 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天全站免登陆