0

I'm trying to display all my user files using java as follows...

 public static String Tree(File file)
{
    if(file.isDirectory()==true)
    {
        File fr[]=file.listFiles();
        No_fold++;
        for(int i=0;i<fr.length;i++)
        {
            Tree(fr[i]);
        }
    }
    else
    {
        X+=file.getPath()+"\n";
        No_files++;
    }
    return X;
}

works perfectly while displaying my documents files... But the following exception is thrown while trying to display the files from C:/Users/username

at com.monster.app.__SHELL137.run(__SHELL137.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at bluej.runtime.ExecServer$3.run(ExecServer.java:725)

Please help me regarding this.. Thnk U

Deepak
  • 93
  • 7
  • So which is line 9, and what have you done to diagnose what might be null? – Jon Skeet Aug 27 '15 at 16:58
  • 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) – seenukarthi Aug 27 '15 at 16:58
  • Please post your whole code and the line number where you get the exception. – Ghazanfar Aug 27 '15 at 17:19

1 Answers1

2

The documentation for File.listFiles() says,

An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

So, there are only 2 cases when the returned value of listFiles() is null

  • The abstract pathname does not denote a directory
  • I/O error occurs

Since you are making sure that file is a directory I suspect that there is an I/O error.

You have said,

works perfectly while displaying my documents files... But the following exception is thrown while trying to display the files from C:/Users/username

I think there is some directory inside C:/Users/username or its subdirectories which is not readable by you and is causing an I/O error.

Ghazanfar
  • 1,419
  • 13
  • 21