import java.util.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class SearchText {
public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter file name: ");
    String file = scan.next();
    scan.close();
    System.out.println("Reading the file named: " + file);
    FileReader fr = null;
    String fileContents = "";
    try {
        fr = new FileReader(file);
    } catch (FileNotFoundException e1) {
        System.out.println("Invalid file name. Terminating.");
    }
    BufferedReader br = new BufferedReader(fr);
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        System.out.println("Searching for: " + line);
        List<String> words = new ArrayList<>();
        while(line != null){
            sb.append(line);
            line = br.readLine();
            words.add(line);
        }
        fileContents = sb.toString();
        List<String> wordsClone = new ArrayList<String>();
        for(String string : words){
            if(string.matches(line)){
                wordsClone.set(wordsClone.indexOf(line), "True");
            }else
                wordsClone.set(wordsClone.indexOf(string), "False");
        }
        System.out.println(words);
    } catch(IOException e){
        e.printStackTrace();
    } catch(NullPointerException e){
        System.out.println("Test");
    } finally {
        try {
            br.close();
        } catch(IOException e){
            e.printStackTrace();
        }
    }//end of try/catch/finally
    }
}
I am trying to search a text file for a String. I made the input or "line" so that it is in the text file, and I want it to replace each element of the cloned array with either "True" or "False" depending on whether or not it matches the String or line. I was receiving a NullPointerException, so I decided to catch that exception and print out test, and low and behold it prints Test. The error is occurring on line 43 which is the line that says: if(string.matches(line)). I also tried printing out the String line prior to the cloning of the arrayList, and it is not null. I cannot figure out what is causing this exception to throw and where line is losing its stored data. Thanks!
