1

I want to build a data structure where I would store all the files from the file-system. For that I have a class directoryNode:

class directoryNode{
    private String name;
    private String path;
    File file;

    //This List Stores the sub-directories of the given Directory.

    private List<directoryNode> subDirectories = new ArrayList<directoryNode>();

    //This List stores the simple files of the given Directory

    private List<String> fileNames = new ArrayList<String>();

    //The Default Constructor.
    directoryNode(File directoryName){
        this.name = directoryName.getName();
        this.path = directoryName.getPath();
        this.file = directoryName;
        //A Function to build this directory.
        buildDirectory();
    }

    File[] filesFromThisDirectory;

    private void buildDirectory(){
        //get All the files from this directory
        filesFromThisDirectory = file.listFiles();
        try{
            for(int i = 0 ; i < filesFromThisDirectory.length ; i++){
                if(filesFromThisDirectory[i].isFile()){
                    this.fileNames.add(filesFromThisDirectory[i].getName());
                } else if(filesFromThisDirectory[i].isDirectory()){
                    directoryNode Dir = new directoryNode(filesFromThisDirectory[i]);
                    this.subDirectories.add(Dir);
                }
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}

My Program Works fine but I get some weird behavior when I don't use the try- Catch block in the buildDirectory() Function. Build Function recursilvely builds the structure for the list of files as written in the code.

when I do:

directoryNode d1 = new directoryNode(new File("/"));

when try-Catch is there , program works fine but if I remove the try catch block : i get an error after executing it for some time: The error that I get is :

 Exception in thread "main" java.lang.NullPointerException
  at directoryNode.buildDirectory(myClass.java:47)
  at directoryNode.<init>(myClass.java:22)
  at directoryNode.buildDirectory(myClass.java:55)
  at directoryNode.<init>(myClass.java:22)
  at directoryNode.buildDirectory(myClass.java:55)
  at directoryNode.<init>(myClass.java:22)
  at myClass.main(myClass.java:75) 

but when I run :

  directoryNode d1 = new directoryNode(new File("/home/neeraj"));

With or without try - catch block , I the program runs fine without any error. Why is this so ? Why do I get different results in these these scenarios?

awksp
  • 11,764
  • 4
  • 37
  • 44
neerajDorle
  • 540
  • 7
  • 21
  • the for-loop in the buildDirectory() function – neerajDorle May 17 '14 at 07:21
  • Why do you use `File`? Are you stuck with Java 6? – fge May 17 '14 at 07:45
  • @fge Just curious, what's the better alternative for Java 7/8? – awksp May 17 '14 at 07:49
  • @user3580294 java.nio.file; `Path`, `Files` etc – fge May 17 '14 at 07:53
  • 1
    @fge How are they better than the old Files? – awksp May 17 '14 at 07:55
  • I am not having any problems with the File stuff, it is something with the Try -catch block or the logic in my program :) – neerajDorle May 17 '14 at 07:57
  • @user3580294 in so many ways a single comment wouldn't tell the whole story; but consider for instance that exceptions such as NotDirectoryException, AccessDeniedException etc exist – fge May 17 '14 at 07:57
  • @neerajDorle still -- if this is Java 7 or better you should drop File; consider for instance that with Files you can Files.walkFileTree() – fge May 17 '14 at 07:59
  • @fge Well that's certainly nicer than before. I'll have to check this out. Thanks! – awksp May 17 '14 at 08:00
  • yeah that is fine, but can you please address my specific question? I am working on linux machine, I feel that unless you understand basics and how basic things works , you can't appreciate how further versions are beneficial – neerajDorle May 17 '14 at 08:01
  • Please have a look at my code and tell me what is that thing that i am missing – neerajDorle May 17 '14 at 08:02
  • You are probably trying and building a directory from a `File` which isn't one; in this case, `.listFiles()` returns `null`! One more reason to use `Path` instead... – fge May 17 '14 at 08:06
  • Also, it will return null if the file is indeed a directory _but you don't have access to it_. `Files.newDirectoryStream()`, on the other hand, would have thrown either of `NotDirectoryException` or `AccessDeniedException`. – fge May 17 '14 at 08:08
  • Thanks a Lot! make this an answer and I will accept it..... That is the reason I am getting the error – neerajDorle May 17 '14 at 08:14
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Raedwald May 17 '14 at 08:55
  • I don't see that my question is a possible duplicate of the mentioned question. It was a specific problem regarding an error that I was having in my code and it has been answered aptly (thanks to fge) and the answer is also accepted. I know the reason why Null pointer exceptions are triggered but once have a look at my question before tagging it duplicate and downvoting it – neerajDorle May 17 '14 at 09:37

1 Answers1

1

The problem is with this line:

    filesFromThisDirectory = file.listFiles();

This will return null if the File object is not a directory... OR if it is but you don't have the necessary privileges.

You must therefore check that filesFromThisDirectory is not null before entering your loop.

But do yourself a favour, drop File and use java.nio.file instead.

fge
  • 119,121
  • 33
  • 254
  • 329