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);
	}
}

Exception in thread "main" java.lang.NullPointerException
at Check.containsFile(Check.java:70)
at Check.searchForFile(Check.java:55)
at Check.searchForFile(Check.java:61)
at Check.main(Check.java:107)

I replaced the old line numbers with the actual line of code

Exception in thread "main" java.lang.NullPointerException
at Check.containsFile --> for (int i = 0; i < files.length; i++) {
at Check.searchForFile --> if (containsFile(files, filename)) {
at Check.searchForFile --> searchForFile(files, filename);
at Check.main --> File fl = searchForFile(rootDir, fn);

Null pointer in first line can only mean "files" is null. Check this by trying to print it immediately before this line. Look at the code that passes it as a parameter to see if/how it could be null

When the method searchForFile() is ran, it prints out that the file is found (if you created it), once this happens the method should terminate but it does not. I don't understand why the method searchForFile() does not terminate. It continues iterating and this is when the nullPointerException is returned.

Quick guess:
The search method is recursive. When you find the file the current invocation terminates, but the invocation that called it is not terminated. I think what you need is that when you find the file, no matter how deep in the recursion stack, the whole stack is terminated.
When you make the recursive call you ignore the return value; maybe you could check the returned value and, if it is the file, immediately return that.

I think I may be a little confused, I thought I was doing that.

You have this loop:

for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					searchForFile(files[i], filename);
				}
			}

This will continue to loop regardless of what the searchForFile sub-call returns. You then exit the loop and return null
You need to exit this loop as soon as searchForFile returns a file, and you need to do that by returning that file, eg

for (int i = 0; i < files.length; i++) {
	if (files[i].isDirectory()) {
		File f =searchForFile(files[i], filename);
                if (f != null) return f;
	}
}

Actually that was only a quick fix. The real problem was that I forgot to account for the base case in the searchForFile() method (when the array "files" is null). Thanks again though.

This article has been dead for over six months. Start a new discussion instead.