My question is not about null pointer exception. My question is about why while making a file search in C: I got null point exception, even I can list the content. I did a class to search for file. Everything work fine if I'm searching in a directory. But if I search in a drive like c or d, i get a nullpointer exception. How can I fix that ?
here's the code :
import java.io.File;
import java.util.ArrayList;
/**
* Created by Marcwe on 12/02/2017.
*/
public class FileSearch {
ArrayList<String> listOfResult = new ArrayList<>(10);
public FileSearch(File searchPath, String theFile) {
ArrayList<String> oneList = new ArrayList<>(10);
listOfResult = searchTheFile(theFile, searchPath, oneList);
for (String x : listOfResult) {
System.out.println(x);
}
};
public ArrayList<String> searchTheFile(String theFileToSearch, File inThePath, ArrayList<String> aList) {
File pathToSearch = inThePath;
if (!(inThePath == null)) {
for (File item : pathToSearch.listFiles()) {
if (!(item == null) && !(item.isFile())) {
System.out.println(item.getAbsolutePath());
aList = (searchTheFile(theFileToSearch, item, aList));
} else {
if (!(item == null)) {
if (item.getName().equalsIgnoreCase(theFileToSearch)) {
aList.add(item.getAbsolutePath());
}
}
}
}
}
return aList;
}
public static void main(String[] args) {
FileSearch searchTest = new FileSearch(new File("C:/"), "test.txt");
}
}