I am working on a program to process data for a computer science assignment.
My program works by prompting the user to enter a CSV file in a specific format that it scans and parses into an array of data that can then be outputted, sorted, or searched. The program can process data from multiple files as long as the dates are the same on every line of the files.
In order to ensure the dates match up, I have created a method named IsValidFile that accepts. When I run the program with a valid file where all the dates match, I get a NullPointerException when calling IsValidFile().
I searched for an answer online on how to prevent NullPointerException with BufferedReader and found these two posts:
NullPointerException and BufferedReader
and
NullPointerException and BufferedReader.
I tried doing what the answers suggested by adding something to only evaluate the readfile method if the value is not null. I included a print statement to see if the if statement was executing. When I ran the program it printed out the statement inside the if statement checking if it was not null but still returned a NullPointerException.
What is causing this error and how would I go about resolving it?
public static boolean IsValidFile(String fileEntry, String originalFile) {
boolean output = true;
try {
BufferedReader bufferforFile1 = new BufferedReader(new FileReader(originalFile));
bufferforFile1.readLine();
String line1 = bufferforFile1.readLine();
String firstDate = line1.split(",")[0];
String line2;
BufferedReader bufferforFile2 = new BufferedReader(new FileReader(fileEntry));
bufferforFile2.readLine();
System.out.println(bufferforFile2);
while ((bufferforFile2.readLine()) != null) {
if(bufferforFile2.readLine()!=null){
System.out.println("Value is not Null!");
line2 = bufferforFile2.readLine();
boolean condition = line2.split(",")[0].equals(firstDate);
System.out.println("Boolean condition is "+condition);
if (!condition) {
System.out.println("Date mismatch: file closed.");
output = false;
return output;
}
}
}
return output;
} catch (IOException e) {
e.printStackTrace();
return output;
}
}