I've created my own recursive file search. When the file is found it is printed that it is found, but the method searchForFile() does not terminate once the file is found, it continues to iterate and I believe this is when the nullPointerException is invoked. I've looked over this and cannot figure out what the problem is. Any help would be appreciated. Thanks
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.Scanner;
public class Check {
public static File searchForFile(File rootDir, String filename){
File files[] = rootDir.listFiles();
if (containsFile(files, filename)) {
return getFile(files, filename);
}
else {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
searchForFile(files[i], filename);
}
}
}
return null;
}
public static boolean containsFile (File files[], String filename) {
String getName;
for (int i = 0; i < files.length; i++) {
getName = files[i].getName();
if (getName.equals(filename))
return true;
}
return false;
}
public static File getFile (File files[], String filename) {
String pathName;
for (int i = 0; i < files.length; i ++) {
pathName = files[i].getName();
if (pathName.equals(filename)) {
System.out.println("File has been retrieved: " + files[i].getAbsolutePath());
return files[i];
}
}
return null;
}
public static void main(String[] args) throws IOException {
//getting the information
String [][] people = new String [100][5];
String [] cells = null;
String line = null;
int col = 0;
String fn = null;
File rootDir = new File("C:\\");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Save the senority list excel spreadshet as a .csv file. You do this buy opening the file and going to save as.\nMake sure you save the file in a location you can remember.\nWhat is the name of your file?: ");
fn = in.readLine();
if (!(fn.contains(".csv"))) {
fn += ".csv";
}
Scanner myScanner = null;
File fl = searchForFile(rootDir, fn);
if (fl == null) {
System.out.println("Program exiting. Error in locating file. Make sure you enter file name correctly.\nPress any key to continue.");
fn = in.readLine();
System.exit(0);
}
myScanner = new Scanner(fl);
}
}