0

I am working on a file explorer. If I want to open another dir and it is empthy, so no files can be listed, it returns a NullPointerException. How can I can rid of this?

    File F = new File(path);
    File L[] = F.listFiles();
    txtPath.setText(path);
    txtFiles.setText("");
    for(int i=0; i<L.length; i++){
        if(L[i].isDirectory()&&!L[i].isHidden()){
            txtFiles.append(L[i].getName().replace(".txt", "")+'\n');
        }
    }

The error appears at the L.length line. In theory, the lenght should be just 0 right? But it just gives me the error. I tried:

  if(L.lenght>0){
        //do stuff
  }

and (the basically same actually):

 if(F.list().length>0){
     //do stuff
 }

How can I test in advance, if it is empthy? Or am I wrong and it returns the error for another reason? Thanks for any help!

wittn
  • 298
  • 5
  • 16
  • Are you sure the path is correct. list() should only return null if the abstract pathname does not denote a directory, or if an I/O error occurs. it should return an empty array if the directory is empty. I'm guessing your path is incorrect. Can you post the path you are using? Remember for windows you should use \\ instead of \ for the path. – Justin Dec 05 '18 at 21:31
  • @Justin Thanks for the quick response. The used path looks the following: – wittn Dec 05 '18 at 21:34
  • C:\Users C:\Users C:\Users\Public C:\Users\Public\Desktop That path is the path i get from the file though, I didnt type it or anything. And it works too, because in the first couple folders the code works just fine, but on some specfic ones it just doesnt.... – wittn Dec 05 '18 at 21:35
  • it might have to do with permissions. Maybe try and allow everyone access temporarily to confirm. – Justin Dec 05 '18 at 21:45
  • @Justin Hm this might be possible, because on some empty folders it works...but I teted it with File.canWrite(); and it returned a true boolean. I cannot see the folder in the dir for some reason, so I dont know how to give permission to it :( – wittn Dec 05 '18 at 21:55
  • Is there maybe a way to exclude folder, you dont have permisson to? Like it pseudo-code something like if(F.isPermittedToAccess==true)? – wittn Dec 05 '18 at 22:00
  • I believe F.canRead() is what you're looking for. – Justin Dec 05 '18 at 22:48
  • I just tried it, but unfortunaly it returns true (so that I can read it) and I still get the error that i dont have permission for it.... – wittn Dec 06 '18 at 17:25
  • Do you still get a NullPointerException or another Excpetion now? – Justin Dec 06 '18 at 18:21
  • still the NullPointerException. I also tried stuff like Files.isWriteable(path) but it also return true. I am positive now that is has something to do with permission, since if I type in the path of the files with which I get the error, Windows tells me I dont have permission to it. – wittn Dec 06 '18 at 18:25
  • You can always do a null check on L before proceeding – Justin Dec 06 '18 at 18:41
  • Yes but it is not null. It has a value and I can actually let java show me the files in it, it just returns an error right after because I cannot accesses them and so I cannor proceed either. – wittn Dec 06 '18 at 18:44
  • Oh, I thought you were getting the nullpointer on F.listFiles(). What method do you get the nullpointer on? – Justin Dec 06 '18 at 19:17
  • In the code all the way above, I get the nullpointer at the L.length line. – wittn Dec 06 '18 at 19:19
  • Solved it. I am just stupid. The null-Test indeed did it. I was just to stupid apparently to type the method in the right way. Thanks anyway for your great help! @Justin – wittn Dec 06 '18 at 20:09

0 Answers0