I am programming code that searches my computer's storage for a specific file (or multiple of the same name) indicated by a starting path, all of which is user console inputted. My code seems to work fine for some testing but then I when I tried using a more general directory (C:\Users\andy) to search for a file, i get this null pointer exception below.
Exception in thread "main" java.lang.NullPointerException
at FindFile.search(FindFile.java:44)
at FindFile.search(FindFile.java:48)
at FindFile.search(FindFile.java:48)
at FindFile.search(FindFile.java:48)
at FindFile.directorySearch(FindFile.java:32)
at Driver.main(Driver.java:33)
The program runs for a quite a bit of time while it searches through my C drive but then after a few seconds I get thrown the exception above. I am totally lost and any help would be appreciated! (sorry i am new at this)
44: for (File temp : filename.listFiles()
48: search(temp);
32: search(dir);
33: fileSearch.directorySearch(targetFile, pathToSearch);
The class that contains the problematic code:
public class Driver {
public static void main(String[] args) {
String targetFile = "";
String pathToSearch = "";
int maxNumber = 0;
Scanner input = new Scanner(System.in);
System.out.println("max number of files to look for");
maxNumber = input.nextInt();
System.out.println("directory to start in");
pathToSearch = input.next();
System.out.println("What is the file name");
targetFile = input.next();
FindFile fileSearch = new FindFile(maxNumber);
try {
fileSearch.directorySearch(targetFile, pathToSearch);
} catch (IllegalArgumentException e) {
System.out.println("you have reach max number of files found");
}
int count = fileSearch.getFileResult().size();
if (count == 0) {
System.out.println("no results of that file was found");
} else {
System.out.println("found " + count + "of file " + targetFile);
for (String match : fileSearch.getFileResult()) {
System.out.println("location: " + match);
}
}
}
}
public class FindFile {
private int maxFiles;
private List<String> fileResult = new ArrayList<String>();
private String nameOfFile;
private int currentNumOfFiles = 0;
/*
* accepts the max number of files to find
*/
public FindFile(int maxNum) {
maxFiles = maxNum;
}
public FindFile() {
}
/*
* @param target file name to look for and directory to start search
*/
public void directorySearch(String target, String directory) {
File dir = new File(directory);
setNameOfFile(target);
if (dir.isDirectory()) {
search(dir);
} else {
System.out.println("not a directory ");
}
}
public void search(File filename) throws IllegalArgumentException {
if (filename.isDirectory()) {
System.out.println("searching directory..." + filename.getAbsoluteFile());
System.out.println();
if (filename.canRead()) {
for (File temp : filename.listFiles()) {
if (currentNumOfFiles == maxFiles) {
throw new IllegalArgumentException();
} else if (temp.isDirectory()) {
search(temp);
} else {
if (getNameOfFile().equals(temp.getName().toLowerCase())) {
fileResult.add(temp.getAbsolutePath().toString());
currentNumOfFiles = getFileResult().size();
}
}
}
} else {
System.out.println("cannot read into this directory");
}
}
}
public int getCount() {
return currentNumOfFiles;
}
public int getMaxFiles() {
return maxFiles;
}
public void setMaxFiles(int maxFiles) {
this.maxFiles = maxFiles;
}
public List<String> getFileResult() {
return fileResult;
}
public String getNameOfFile() {
return nameOfFile;
}
public void setNameOfFile(String nameOfFile) {
this.nameOfFile = nameOfFile;
}
public int getCurrentNumOfFiles() {
return currentNumOfFiles;
}
public void setCurrentNumOfFiles(int currentNumOfFiles) {
this.currentNumOfFiles = currentNumOfFiles;
}
}