0

I am trying to find all of the files that are named, in the directory that I have chosen. The code that I have works when I do something like C:\Program Files, or C:\Users. But when I do c:/ I get stuck in the recycle bin and get a java.lang.NullPointerException and stops at c:\$Recycle.Bin\S-1-5-21-1478355014-127360780-1969717230-1002144.

public void DirectorySerch(String target, String dirName){
    File f = new File(dirName); System.out.println("H");
    if(!f.isDirectory()){
        throw new IllegalArgumentException("that is not a valid directory");
    }
    for(File folderItem : f.listFiles()){
        if(folderItem.isDirectory()){
            System.out.println(folderItem.getAbsolutePath());
            if(!folderItem.equals("")){
                DirectorySerch(target,folderItem.getPath());
            }
            // Return the result if it is not empty
            /*  if (!result.equals(folderItem.getName())){
            files[filesFounfd] = folderItem.getAbsolutePath();
            filesFounfd++;
            }*/
        }else{
            if(folderItem.getName().equals(target)){
                files[filesFounfd] = folderItem.getAbsolutePath();
                System.out.println(folderItem.getAbsolutePath());
                filesFounfd++;
            }
        }        
    }
}

What can I do to not get this issue as it works in cases when it does not have to deal with the recycle bin?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • How about adding `if("$Recycle.Bin".equals(dirName)) return;` to the beginning of your method? – Jonny Henly Nov 11 '16 at 01:47
  • Your code doesn't compile - several variable declarations are missing. You want help, so you should make it easier for people to help you by providing code that can be compiled and executed, with sample input that demonstrates your problems. You also never mentioned where the NullPointerException is from. Chances are, if you have done that, you'd have solved the problem on your own already. – KC Wong Nov 11 '16 at 02:06

2 Answers2

1
 for(File folderItem : f.listFiles()){

The problem is here. listFiles() can return null, and all this syntax can do with that is throw an NPE. Change to:

 File[] files = f.listFiles();
 if (files != null) {
    for(File folderItem : files){
bmargulies
  • 97,814
  • 39
  • 186
  • 310
user207421
  • 305,947
  • 44
  • 307
  • 483
-1

so it turns out you can fix this by making a try catch around the for loop

    public void DirectorySerch(String target, String dirName) {
    File f = new File(dirName);
    System.out.println("H");
    if (!f.isDirectory()) {
        throw new IllegalArgumentException("that is not a valid directory");
    }
    try {
        for (File folderItem : f.listFiles()) {
            if (folderItem.isDirectory()) {
                System.out.println(folderItem.getAbsolutePath());
                if (!folderItem.equals("")) {
                    DirectorySerch(target, folderItem.getPath());
                }
            } else {
                if (folderItem.getName().equals(target)) {
                    files[filesFounfd] = folderItem.getAbsolutePath();
                    System.out.println(folderItem.getAbsolutePath());
                    filesFounfd++;
                }
            }
        }
    }catch(NullPointerException sd){

    }
}
  • Assuming that every `NullPointerException` can be ignored, not just the one that caused the problem. Not good practice. – user207421 Nov 11 '16 at 02:14