简体   繁体   English

扫描目录时java.lang.NullPointerException

[英]java.lang.NullPointerException when scanning directories

I have a very simple method that scans a directory structure to perform a check. 我有一个非常简单的方法来扫描目录结构以执行检查。 The scanning looking like this: 扫描看起来像这样:

File file = new File(initpath);

for(File hex : file.listFiles(new HexagonNameFilter())) {

    for(File wall : hex.listFiles()) {

        for(File shelf : wall.listFiles()) {

            for(File book : shelf.listFiles()) {

                // Perform some actual work
            }
        }
     }
 }

The method is called lots of times during the execution of the program. 在程序执行期间多次调用该方法。

Inconsistently (meaning, at some unpredictable point in the scanning process), I get a java.lang.NullPointerException with the stack trace pointing at one of the for statements (which one it is is also inconsistent). 不一致(意思是,在扫描过程中的一些不可预测的点),我得到一个java.lang.NullPointerException,其中堆栈跟踪指向其中一个for语句(它的一个也是不一致的)。 This is not enlightening. 这不具有启发性。 I was thinking of passing FilenameFilters to the three listFiles() calls, but can't see how that would help the issue. 我正在考虑将FilenameFilters传递给三个listFiles()调用,但无法看到这将如何帮助解决问题。

You should verify that you are calling this method on a directory. 您应该验证是否在目录上调用此方法。 Otherwise, it returns null. 否则,它返回null。

listFiles listFiles

public File[] listFiles() public File [] listFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. 返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件。

If this abstract pathname does not denote a directory, then this method returns null . 如果此抽象路径名不表示目录,则此方法返回null Otherwise an array of File objects is returned, one for each file or directory in the directory. 否则返回一个File对象数组,一个用于目录中的每个文件或目录。 Pathnames denoting the directory itself and the directory's parent directory are not included in the result. 表示目录本身的路径名和目录的父目录不包含在结果中。 Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. 每个生成的抽象路径名都是使用File(File,String)构造函数从此抽象路径名构造的。 Therefore if this pathname is absolute then each resulting pathname is absolute; 因此,如果此路径名是绝对的,那么每个结果路径名都是绝对的; if this pathname is relative then each resulting pathname will be relative to the same directory. 如果此路径名是相对的,则每个结果路径名将相对于同一目录。

There is no guarantee that the name strings in the resulting array will appear in any specific order; 无法保证结果数组中的名称字符串将以任何特定顺序出现; they are not, in particular, guaranteed to appear in alphabetical order. 特别是,它们不保证按字母顺序出现。

Returns: An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. 返回:一个抽象路径名数组,表示此抽象路径名表示的目录中的文件和目录。 The array will be empty if the directory is empty. 如果目录为空,则数组将为空。 Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs. 如果此抽象路径名不表示目录,或者发生I / O错误,则返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM