文件java.lang.NullPointerException。错误

时间:2017-03-21 01:13:27

标签: java file exception nullpointerexception directory

我在下面有以下代码,用于列出特定文件夹/目录中的所有文件。

对于mac来说这很有效,但是当涉及到Windows时,我得到了一个java.lang.NullPointerException。我不完全确定如何修复它,我知道它意味着一个目录File变量在被放入函数时作为Null传递。但我不知道如何检查目录是否为空以及为什么它只在该特定目录上创建错误,因为它适用于所有其他目录,而它不起作用的目录只是Windows上的常规文档目录。我对显示java.lang.NullPointerException错误的三行做了一个小评论。

我还尝试通过包围文件列表功能来修复它,以检查文件夹是否为空。但是这不起作用,因为它已经很晚了,null错误已经在File变量声明中发生了。

public static void main() throws IOException {

        if (isMac()) {
            listFilesForFolderMac(folderMac1);
            listFilesForFolderMac(folderMac2);
            listFilesForFolderMac(folderMac3);
            listFilesForFolderMac(folderMac4);
            listFilesForFolderMac(folderMac5);
            listFilesForFolderMac(folderMac6);
            listFilesForFolderMac(folderMac7);
        } else if (isWindows()) {
            listFilesForFolderWin(folderWin1);
            listFilesForFolderWin(folderWin2);
            listFilesForFolderWin(folderWin3);
            listFilesForFolderWin(folderWin4);
            listFilesForFolderWin(folderWin5);
            listFilesForFolderWin(folderWin6);
        }

    }

    public static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }

    public static boolean isMac() {
        return (OS.indexOf("mac") >= 0);
    }

public static void listFilesForFolderMac(final File folder) throws IOException {
        PrintWriter writToDoc = new PrintWriter(new FileWriter("/Users/" + username + "/Documents/files.txt",true));
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                listFilesForFolderMac(fileEntry);
            } else {
                writToDoc.println(fileEntry.getName());
            }
        }
        writToDoc.close();
    }

    public static void listFilesForFolderWin(final File folder) throws IOException {
        PrintWriter writToDoc = new PrintWriter(new FileWriter("c:\\users\\" + username + "\\Documents\\files.txt",true));
        for (final File fileEntry : folder.listFiles()) { //Error here
            if (fileEntry.isDirectory()) {
                    listFilesForFolderWin(fileEntry); //Error here
            } else {
                writToDoc.println(fileEntry.getName());
            }
        }
        writToDoc.close();
    }

    final static File folderMac1 = new File("/Users/" + username + "/Pictures");
    final static File folderMac2 = new File("/Users/" + username + "/Documents");
    final static File folderMac3 = new File("/Users/" + username + "/Movies");
    final static File folderMac4 = new File("/Users/" + username + "/Music");
    final static File folderMac5 = new File("/Users/" + username + "/Downloads");
    final static File folderMac6 = new File("/Users/" + username + "/Applications");
    final static File folderMac7 = new File("/Users/" + username + "/Desktop");


    final static File folderWin1 = new File("C:\\Users\\" + username + "\\Desktop");
    final static File folderWin2 = new File("C:\\Users\\" + username + "\\Downloads");
    final static File folderWin3 = new File("C:\\Users\\" + username + "\\Documents"); //Error here
    final static File folderWin4 = new File("C:\\Users\\" + username + "\\Pictures");
    final static File folderWin5 = new File("C:\\Users\\" + username + "\\Music");
    final static File folderWin6 = new File("C:\\Users\\" + username + "\\Videos");

我收到以下错误。

Exception in thread "main" java.lang.NullPointerException
    at script.MyClass.listFilesForFolderWin(MyClass.java:200)
    at script.MyClass.listFilesForFolderWin(MyClass.java:202)
    at script.MyClass.main(MyClass.java:155)

3 个答案:

答案 0 :(得分:0)

我已经搞乱了Java所拥有的文件命令,通常当你得到NullPointerExcpetion时,这意味着你给它的目录是错误的。我没有在Windows上运行您的代码,但是对于该平台,您需要做的就是遵循与Mac路径相同的文件结构。以下是您的第一个变量:

final static File folderWin1 = new File("C:/Users/" + username + "/Pictures");

所有其他变量应遵循相同的结构。我希望这可以解决你的问题。

答案 1 :(得分:0)

Windows中有一些文件夹,Java可以看到,但没有看到,以及它看到的一些文件目录。要解决您的问题,请在尝试列出文件之前进行检查:

if (fileEntry.isDirectory()&&fileEntry.listFiles()!=null) {
    listFilesForFolderWin(fileEntry);
} 

答案 2 :(得分:0)

java.util.File无法处理Window的“联结”(一种符号链接),例如“我的文档”和“图片”

您需要切换到NIO2 API并使用Paths API。

参见Walking the File Tree实际上有一个处理符号链接的例子

从上面的链接中提取

@Override
public FileVisitResult visitFile(Path file,
                               BasicFileAttributes attr) {
    if (attr.isSymbolicLink()) {
        System.out.format("Symbolic link: %s ", file);
    } else if (attr.isRegularFile()) {
        System.out.format("Regular file: %s ", file);
    } else {
        System.out.format("Other: %s ", file);
    }
    System.out.println("(" + attr.size() + "bytes)");
    return CONTINUE;
}

您可能还想查看Links, Symbolic or Otherwise了解更多详情