0
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!

Fall0ut
  • 71
  • 1
  • 12
  • So from your debugging, ***what*** variable is null when the exception is thrown? – Hovercraft Full Of Eels Dec 18 '17 at 02:41
  • While would you attempt to create a `BufferedReader` after the `FileReader` generate a `FileNotFoundException`? – MadProgrammer Dec 18 '17 at 02:41
  • It looks like `line` ***should*** be null at that location, since just before the exception is thrown, you read in a while loop **until line is null**. – Hovercraft Full Of Eels Dec 18 '17 at 02:41
  • @HovercraftFullOfEels I believe its replacing "line" and making it null. – Fall0ut Dec 18 '17 at 02:42
  • By the time the code reaches `if(string.matches(line)){`, `line` is `null`, as it's the required exit condition from the `while-loop` - `while(line != null){` ... – MadProgrammer Dec 18 '17 at 02:43
  • @MadProgrammer: exactly as I mentioned above. – Hovercraft Full Of Eels Dec 18 '17 at 02:43
  • @Fall0ut I believe `line` is `null` because that's the only way the `while-loop` before it would exit – MadProgrammer Dec 18 '17 at 02:43
  • @HovercraftFullOfEels Comments updated AFTER I posted :P – MadProgrammer Dec 18 '17 at 02:43
  • 1
    @MadProgrammer: and :p to you too sir! :) Regardless, this question is a duplicate of many similar. – Hovercraft Full Of Eels Dec 18 '17 at 02:44
  • @HovercraftFullOfEels is there any way to go about fixing this? This is solely for my own practice, Im trying to push myself as a student and I have not used many of these things. Not asking for the answer because I want to figure it out, I've been at this for hours and its driving me crazy – Fall0ut Dec 18 '17 at 02:46
  • 1
    @Fall0ut Don't use `line` to read the contents of the file, as that seems to be the value you are attempting to find, it doesn't make sense to use it the way you are – MadProgrammer Dec 18 '17 at 02:47
  • Yep, the lines variable is guaranteed to be null where you use it, and it makes no sense using it as you're doing. The solution to this is to do more in depth rubber-duck debugging -- making sure that the code you're writing makes logical sense. – Hovercraft Full Of Eels Dec 18 '17 at 02:48
  • @MadProgrammer my end goal is to search for the string that is on the first line of the program. That is the only reason I was using line. – Fall0ut Dec 18 '17 at 02:48
  • @MadProgrammer You should *not* close a `Scanner` that is reading from `System.in`. – user207421 Dec 18 '17 at 02:50
  • @Fall0ut Okay, pull the first element out of the `ArrayList` after you've finished reading it – MadProgrammer Dec 18 '17 at 02:51
  • You **not** should close a Scanner which is reading from stdin – MadProgrammer Dec 18 '17 at 02:51
  • @EJP Apparently my fingers can't keep up with my thinking - thank you, corrected – MadProgrammer Dec 18 '17 at 02:52
  • Alright thank you guys, Ill keep at it, sorry Im still pretty new to Java and Im really trying to push myself. I appreciate the help even though some of the questions may be amateur! – Fall0ut Dec 18 '17 at 02:52
  • Suggestion: use a debugger to see what your code is doing, especially if it is misbehaving. Make sure you do this **before** coming here. – Hovercraft Full Of Eels Dec 18 '17 at 02:54
  • @HovercraftFullOfEels okay will do thank you, like I said I'm rather new so other than seeing my issues in the console I dont know many of the tools yet – Fall0ut Dec 18 '17 at 02:56

0 Answers0