0

I'm trying to downgrade an application from Java 7 to Java 6 for the sake of compatibility. However, with Jave 6's listFiles() I get a NullPointerException for the c drive on Windows machines (and isHidden() returns true). Since Java 7's FileTreeWalker worked nicely, I'd assume it's not a Windows issue, is it? I used this piece of code:

public class Filewalker {

    public void walk( String path ) {

        File root = new File( path );
        File[] list = root.listFiles();

        for ( File f : list ) {
            if ( f.isDirectory() ) {
                walk( f.getAbsolutePath() );
                System.out.println( "Dir:" + f.getAbsoluteFile() );
            }
            else {
                System.out.println( "File:" + f.getAbsoluteFile() );
            }
        }
    }

    public static void main(String[] args) {
        Filewalker fw = new Filewalker();
        fw.walk("c:\\" );
    }
}

Bob

Community
  • 1
  • 1
Bob
  • 141
  • 1
  • 8
  • Which path did you exactly use, to list your files? – SomeJavaGuy Jan 21 '13 at 09:10
  • We can't really help you unless you show us the code that causes the problem. (Showing us code that works is ... pointless) – Stephen C Jan 21 '13 at 09:13
  • OK, got it. the problem is that `listFiles("c:\\")` returns `null`. The NPE only occurs because you don't test for null. – Andreas Dolk Jan 21 '13 at 09:16
  • you got 3 Options for this NPE. 1. your path does not exist 2. listfiles trys to list the Files of a file instead of a directory 3. An I/O happend – SomeJavaGuy Jan 21 '13 at 09:21
  • But even if I test for null, that doesn't get me the directory listing of the c drive. Strange enough, the code sample *is* for the c drive. – Bob Jan 21 '13 at 09:22
  • even so, you should use "C:/" instead of "C:\\", because the second solution is windows specific, the first one should be unsiversal – SomeJavaGuy Jan 21 '13 at 09:24
  • Did you check your permissions on C:/ drive. Sometimes if you are not an Administrative user. These sort of things can happen. – shazin Jan 21 '13 at 09:34
  • @Bob To access files on `C:` You need to have special permission life an admin. – Smit Jan 21 '13 at 09:56
  • @smit: But how does it come that Java 7 doesn't need these permissions? – Bob Jan 21 '13 at 12:02

1 Answers1

2

Try Apache Commons IO, they might offer a cross JDK solution for your case.

[Update] I ran your code and after checking with the JDK Documentation, listFiles returns null for paths that do not denote a directory, now looking at the code, despite the fact that you are checking if the current File is a directory prior to recursively calling walk, it turns out that at certain times a dir is mistaken for a file, It might be due to access permissions on Windows I'm not really quite sure why!

A note that lead me to think it has to do with permissions is that the dir that produced the NullPointerException was 'C:\Documents and Settings', calling listFiles on this path, returns null.

A temporary fix for the case would be to check if the file list is null or not.

Waleed Almadanat
  • 1,027
  • 10
  • 24
  • As a fallback option: yes, but I'm right now I'm really just trying to find out what's wrong the c drive here. – Bob Jan 21 '13 at 09:28