ListFiles () returns null if it is not. It used to work fine until recently and has not been changed

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 login screen every minute. listFiles () returns null for dir, and I get a NullPointerException for for (File img: dir.listFiles ()). I thought there might be a file permissions issue, so I tried changing the path to the directory that I have on my desktop and it works fine. Therefore, I assume that I can not access the system files, because my program does not have enough rights. Let me also clarify that this code worked until recently. It has not been changed. I just found out that my input in the background is no longer changing. Even when the program really worked, I could not change the file name when I started the program through Eclipse, but I would export it as a .jar and schedule it using the task scheduler with the highest privileges to give it administrator privileges, and it worked without problems until recently. I also tried to ignore the errors, thinking that they were related to access rights, and tried to run my executable jar with the highest privileges using the Task Scheduler, as well as using a batch file. I even tried to start the jar using cmd, which I opened with administrator privileges, but said nothing that NullPointerException in cmd. I’m kind of lost and I will be grateful for any help.

+6
source share
4 answers

On Windows 7 (and later), the process should run with elevated privileges to write to C:/Windows and similar directories. But if it was a problem, it would lead to another error message.

What I suspect: When starting a 32-bit JVM under 64-bit Windows, a new File("C:/Windows/System32") will point to C:\Windows\SysWOW64 and there is no info Folder under C:\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()); } } 

works fine with the 64-bit JRE and throws a NullPointerException under the 32-bit-JRE on the 64-bit version of Windows 7.

So, maybe you or another application recently installed the 32-bit version or changed your path to the 32-bit jre and thereby violated your application.

+4
source

Do not compare strings with ==

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

Use equals

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

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

It "returns null if this abstract path name does not indicate a directory, or if an I / O error occurs." Thus, either this is not a directory or an I / O error has occurred.

You need to protect the defense from this possibility, and not just contradict the actual assertion that "it should not." He did it, and he can.

Maybe you should remove the trailing /.

+1
source

If you get a NullPointerException in the for(File img : dir.listFiles()) , it means that the dir variable is null , and you cannot call it listFiles() . Have you tried debugging it and see if the directory path has changed or is dir really installed correctly?

-2
source

Source: https://habr.com/ru/post/959225/


All Articles