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

4

11
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.

Scincoid answered 3/12, 2013 at 9:20 Comment(1)
I have this problem on JDK 11Tristichous
P
7

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

What I suspect: When running a 32-bit JVM under 64-bit Windows 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());
    }
}

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

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.

Pangermanism answered 3/12, 2013 at 9:50 Comment(2)
That was it. I'm running a 64bit Windows 7 and I recently changed my jre from 64bit to 32bit. Thanks a lot!Scincoid
Just came across the same problem, but with a 64-bit JDK on 64-bit Win 7, where C:\Documents and Settings returns true to isDirectory(), but returns null (not even an empty File array!) to a listFiles() call in an RCP desktop app. All you can do is add a check, I guess...Baines
C
1

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

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.

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 /.

Cretaceous answered 3/12, 2013 at 9:26 Comment(2)
I tried dir.isDirectory() and it returns false. Appearently my directory isn't a directory. Which doesn't make sense to me. I tried both with / and without. And it's a legitimate directory and there shouldn't be errors in the path name since I copy pasted it and replaced \ with /. And what I don't understand it used to work perfectly until recently.Scincoid
So it isn't a directory, at least to you. Maybe it's a permission problem.Cretaceous
T
1

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"))
Trinity answered 3/12, 2013 at 9:29 Comment(0)
G
-5

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

Gillispie answered 3/12, 2013 at 9:25 Comment(5)
'Most likely?' It can't mean anything else.Cretaceous
dir is indeed null. I tried checking if it was a directory with dir.isDirectory() which returns false. Which I can't explain because it is a directory and the path name is correct. And besides it used to work the way it is until recently. Neither the directory nor the code were modified.Scincoid
I'll edit the very unfortunate term 'most likely' out then, excuse my inexpertise in the English language.Gillispie
@Gillispie Do you have some evidence about who downvoted this question? Or are you just speculating pointlessly?Cretaceous
@EJP It was speculation. Since you were the only one commenting negatively on my answer that was my deduction but apparently not pointless since it got me where/what I wanted :) but please excuse me if I came across aggressive.Gillispie

© 2022 - 2024 — McMap. All rights reserved.