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?

16
  • the for-loop in the buildDirectory() function Commented May 17, 2014 at 7:21
  • Why do you use File? Are you stuck with Java 6? Commented May 17, 2014 at 7:45
  • @fge Just curious, what's the better alternative for Java 7/8? Commented May 17, 2014 at 7:49
  • @user3580294 java.nio.file; Path, Files etc Commented May 17, 2014 at 7:53
  • 1
    @fge How are they better than the old Files? Commented May 17, 2014 at 7:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.