简体   繁体   中英

How to prevent NullPointer Exception from occuring in Java when using BufferedReader after making sure value is not null?

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

You keep calling method readLine but you never assign the value returned by that method to any variable. Every time you call method readLine , it attempts to read the next line in the file. Let's assume that the readLine in the if statement reads the last line of the file

if(bufferforFile2.readLine()!=null) {
    System.out.println("Value is not Null!");

then the condition would be true and the code would print

Value is not Null!

However the next line of code, ie

line2 = bufferforFile2.readLine();

tries to read the next line of the file but there isn't a next line because the previous call to readLine read the last line of the file. So BOOM, you get NullPointerException .

Read a single line of the file in each iteration of the while loop. Consider the following code.

String line2 = bufferforFile2.readLine();
while (line2 != null) {
    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;
    }
    line2 = bufferforFile2.readLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM