Java listFiles() 在不应该返回时返回 null。直到最近它都可以正常工作并且没有被修改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20347161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:13:58  来源:igfitidea点击:

listFiles() returns null when it shouldn't. It used to work properly until recently and hasn't been modified

javabatch-filejarnullpointerexceptionaccess-rights

提问by Ryuu

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    while(true)
    {
        ArrayList<File> wallpapers = new ArrayList<File>();
        File dir = new File("C:/Windows/System32/oobe/info/backgrounds/");
        if(dir.listFiles() == null)
            System.out.println("Empty");
        for(File img : dir.listFiles())
        {
            if(img.getName().endsWith(".jpg") && img.getName() != "backgroundDefault.jpg")
                wallpapers.add(img);
        }
        File current = new File("C:/Windows/System32/oobe/info/backgrounds/backgroundDefault.jpg");
        int i = 1;
        for(File img : wallpapers)
        {
            File f = new File("C:/Windows/System32/oobe/info/backgrounds/"+ i++ +".jpg");
            current.renameTo(f);
            File file = new File("C:/Windows/System32/oobe/info/backgrounds/backgroundDefault.jpg");
            img.renameTo(file);
            Thread.sleep(60000);
        }

    }
}   }
}

This code changes the background image of the Windows Log In screen every minute. listFiles() returns null for dir and I get a NullPointerException on for(File img : dir.listFiles()). I thought there may be a problem with file rights so I tried to change the file path to a directory I have on my Desktop and it works fine. So I'm assuming I can't access system files because my program doesn't have enough rights. Let me also precise that this code used to work fine until recently. It hasn't been modified. I just found out that my Log In Wallpaper doesn't change anymore. Even when the program did work I couldn't modify the file name when I launched the program through Eclipse but I would export it as .jar and schedule it with Task Scheduler with highest privileges to give it admin rights and it worked without any problems until recently. I also tried ignoring the errors thinking they were related to access rights and tried to launch my executable jar with highest privileges through Task Scheduler and also using a batch file. I even tried launching the jar through a cmd I opened with Administrator Rights to no avail it still says NullPointerException in the cmd. I'm kind of lost and would appreciate any help.

此代码每分钟更改一次 Windows 登录屏幕的背景图像。listFiles() 为 dir 返回 null,我在 for(File img : dir.listFiles()) 上得到 NullPointerException。我认为文件权限可能有问题,所以我尝试将文件路径更改为我桌面上的目录,并且它工作正常。所以我假设我无法访问系统文件,因为我的程序没有足够的权限。让我也确切地说,这段代码直到最近都可以正常工作。它没有被修改。我刚刚发现我的登录壁纸不再改变。即使该程序运行正常,当我通过 Eclipse 启动该程序时,我也无法修改文件名,但我会将其导出为 . jar 并使用具有最高权限的任务计划程序安排它以授予它管理员权限,并且直到最近它都没有任何问题。我还尝试忽略错误,认为它们与访问权限有关,并尝试通过任务计划程序并使用批处理文件以最高权限启动我的可执行 jar。我什至尝试通过我以管理员权限打开的 cmd 启动 jar 无济于事,它仍然在 cmd 中显示 NullPointerException。我有点迷茫,希望得到任何帮助。

采纳答案by piet.t

On Windows 7 (and later) the process will have to run with elevated privileges to writeto C:/Windowsand similar directories. But if that was the problem it would result in a different error message.

在Windows 7(及更高版本)的过程中必须与提升权限运行写入C:/Windows和类似的目录。但如果这是问题所在,则会导致不同的错误消息。

What I suspect: When running a 32-bit JVM under 64-bit Windows new File("C:/Windows/System32")will point to C:\Windows\SysWOW64and there is no info-Folder under C:\Windows\SysWOW64\oobe

我怀疑:在 64 位 Windows 下运行 32 位 JVM 时new File("C:/Windows/System32")会指向C:\Windows\SysWOW64并且info下没有-FolderC:\Windows\SysWOW64\oobe

As a test:

作为测试:

public static void main(String[] args) {
    File sysdir = new File("C:/Windows/System32/oobe/info");
    for(File file:sysdir.listFiles()) {
        System.out.println(file.getName());
    }
}

runs fine with 64-bit-JRE and throws NullPointerExceptionunder 32-bit-JRE on Windows 7 64-bit.

在 64 位 JRE 上运行良好,在 64 位NullPointerExceptionWindows 7 上在 32 位 JRE 下运行。

So perhaps you or another application recently installed a 32-bit-jre or changed your path to point to a 32-bit-jre and thus broke your application.

因此,也许您或其他应用程序最近安装了 32 位 jre 或更改了路径以指向 32 位 jre,从而破坏了您的应用程序。

回答by Octoshape

If you get a NullPointerExceptionon the line for(File img : dir.listFiles())that means that the variable diris nulland you can't call listFiles()on it. Have you tried debugging it and see if maybe the directory path has changed or that diris in fact set properly?

如果您在线上得到一个NullPointerException,则for(File img : dir.listFiles())意味着该变量dirnull并且您不能调用listFiles()它。您是否尝试过调试它并查看目录路径是否已更改,或者dir实际上是否已正确设置?

回答by user207421

listFiles() returns null for dir and I get a NullPointerException on for(File img : dir.listFiles()).

listFiles() 为 dir 返回 null,我在 for(File img : dir.listFiles()) 上得到 NullPointerException。

It "returns null if this abstract pathname does not denote a directory, or if an I/O error occurs". So either it isn't a directory, or an I/O error occurred.

它“如果此抽象路径名不表示目录,或者发生 I/O 错误,则返回 null”。所以要么它不是目录,要么发生了 I/O 错误。

You need to code defensively against this possibility rather than just contra-factually assert that "it shouldn't". It did, and it can.

您需要针对这种可能性进行防御性编码,而不仅仅是反事实断言“它不应该”。它做到了,而且可以。

Maybe you should remove the trailing /.

也许你应该删除尾随 /.

回答by Paul Samsotha

Don't compare strings with ==

不要将字符串与 ==

if(img.getName().endsWith(".jpg") && img.getName() != "backgroundDefault.jpg")

Use equals

使用等于

if(img.getName().endsWith(".jpg") && !(img.getName()).equals("backgroundDefault.jpg"))