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!