0

I have written this program to compare 2 files. They are 500mb to 2.8gb in size and are created every 6 hours. I have 2 files from 2 sources (NMD and XMP). They are broken up into lines of text that have fields separated by the pipe(|) character. Each line is a single record and may be up to 65,000 characters long. The data is about TV shows and movies, showing times and descriptive content. I have determined that any particular show or movie has a minimum of 3 pieces of data that will uniquely identify that show or movie. IE: CallSign, ProgramId and StartLong. The two sources for this data are systems called NMD and XMP hence that acronym added to various variables. So my goal is to compare a file created by NMD and one created by XMP and confirm that everything that NMD produces is also produced by XMP and that the data in each matched record is the same.

What I am trying to accomplish here is this: 1. Read the NMD file record by record for the 3 unique data fields. 2. Read the XMP file record by record and look for a match for the current record in the NMD file. 3.The NMD file should iterate one record at a time. Each NMD record should then be searched for in the entire XMD file, record by record for that same record. 4. Write a log entry in one of 2 files indicating success or failure and what that data was.

I have an NPE issue when I reach the end of the testdataXMP.txt file. I assume the same thing will happen for testdataNMD.txt. I'm trying to break out of the loop right after the readLine since the epgsRecordNMD or epgsRecordXMP will have just reached the end of the file if it at that point in the file. The original NPE was for trying to do a string split on null data at the end of the file. Now I'm getting an NPE here according to the debugger.

                    if (epgsRecordXMP.equals(null)) {
                        break;
                    }

Am I doing this wrong? If I'm really at the end of the file, the readLine ought to return null right?

I did it this way too, but to my limited experience they feel like they are effectively the same thing. It too threw an NPE.

if (epgsRecordXMP.equals(null)) break;

Here's the code...

public static void main(String[] args) throws java.io.IOException {

    String epgsRecordNMD = null;
    String epgsRecordXMP = null;
    BufferedWriter logSuccessWriter = null;
    BufferedWriter logFailureWriter = null;
    BufferedReader readXMP = null;
    BufferedReader readNMD = null;
    int successCount = 0;


    readNMD = new BufferedReader(new FileReader("d:testdataNMD.txt"));
    readXMP = new BufferedReader(new FileReader("d:testdataXMP.txt"));

    do {

            epgsRecordNMD = readNMD.readLine();
            if (epgsRecordNMD.equals(null)) {
                break;
            }
            String[] epgsSplitNMD = epgsRecordNMD.split("\\|");
            String epgsCallSignNMD = epgsSplitNMD[0];
            String epgsProgramIdNMD = epgsSplitNMD[2];
            String epgsStartLongNMD = epgsSplitNMD[9];
            System.out.println("epgsCallsignNMD: " + epgsCallSignNMD + " epgsProgramIdNMD: " + epgsProgramIdNMD + " epgsStartLongNMD: " + epgsStartLongNMD );


            do {


                    epgsRecordXMP = readXMP.readLine();
                    if (epgsRecordXMP.equals(null)) {
                        break;
                    }
                    String[] epgsSplitXMP = epgsRecordXMP.split("\\|");
                    String epgsCallSignXMP = epgsSplitXMP[0];
                    String epgsProgramIdXMP = epgsSplitXMP[2];
                    String epgsStartLongXMP = epgsSplitXMP[9];
                    System.out.println("epgsCallsignXMP: " + epgsCallSignXMP + " epgsProgramIdXMP: " + epgsProgramIdXMP + " epgsStartLongXMP: " + epgsStartLongXMP);


                    if (epgsCallSignXMP.equals(epgsCallSignNMD) && epgsProgramIdXMP.equals(epgsProgramIdNMD) && epgsStartLongXMP.equals(epgsStartLongNMD)) {
                        logSuccessWriter = new BufferedWriter (new FileWriter("d:success.log", true));
                        logSuccessWriter.write("NMD match found in XMP " + "epgsCallsignNMD: " + epgsCallSignNMD + " epgsProgramIdNMD: " + epgsProgramIdNMD + " epgsStartLongNMD: " + epgsStartLongNMD);
                        logSuccessWriter.write("\n");
                        successCount++;
                        logSuccessWriter.write("Successful matches: " + successCount);
                        logSuccessWriter.write("\n");
                        logSuccessWriter.close();
                        System.out.println ("Match found");
                        System.out.println ("Successful matches: " + successCount);

                    }

                } while (epgsRecordXMP != null); 

                readXMP.close();

                if (successCount == 0) {
                    logFailureWriter = new BufferedWriter (new FileWriter("d:failure.log", true));
                    logFailureWriter.write("NMD match not found in XMP" + "epgsCallsignNMD: " + epgsCallSignNMD + " epgsProgramIdNMD: " + epgsProgramIdNMD + " epgsStartLongNMD: " + epgsStartLongNMD);
                    logFailureWriter.write("\n");
                    logFailureWriter.close();
                    System.out.println ("Match NOT found");
                }

       } while (epgsRecordNMD != null);
        readNMD.close();

    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JavaNoob
  • 17
  • 3

1 Answers1

0

You should not make this:

if (epgsRecordXMP.equals(null)) {
    break;
}

If you want to know if epgsRecordXMPis null then the if should be like this:

if (epgsRecordXMP == null) {
    break;
}

To sum up: your app throws NPE when try to call equals method in epgsRecordXMP.

Dazak
  • 1,011
  • 2
  • 9
  • 17
  • Thanks, but why does it throw the NPE? I used that exact same command to determine if a variable was equal to another variable of which both are strings. I'm 100% uninitiated...so I don't know much yet, but I thought that == was boolean and that boolean couldn't be done for strings. – JavaNoob Jul 19 '17 at 23:05
  • to know more of NPE look at [this](https://stackoverflow.com/a/218390/5602069), and == is not for booleans, it is for primitives, you can look at [this](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) – Dazak Jul 19 '17 at 23:07
  • well the NPE is gone, but the debugger does this now. – JavaNoob Jul 19 '17 at 23:17
  • Source not found. – JavaNoob Jul 19 '17 at 23:17
  • Then there is a "Edit Source Lookup Path..." button and that opens a window asking me to edit the source lookup path. There is default and my project path or C:\\\\\RK_java. What's the right thing here? – JavaNoob Jul 19 '17 at 23:20
  • However I get this now....Exception in thread "main" java.io.IOException: Stream closed at java.io.BufferedReader.ensureOpen(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at java.io.BufferedReader.readLine(Unknown Source) at FileParse.main(FileParse.java:35) – JavaNoob Jul 19 '17 at 23:21
  • That error in what line happens? – Dazak Jul 19 '17 at 23:22
  • all I did was exactly what you suggested and change .equals(null) to == null. – JavaNoob Jul 19 '17 at 23:22
  • Well that means that you have another error in the code, but at least the NPE is gone – Dazak Jul 19 '17 at 23:23
  • The error happens right after iterating to the second record in testdataNMD.txt which is in the outer do/while loop – JavaNoob Jul 19 '17 at 23:23
  • This is the first record in testdataNMD.txt. – JavaNoob Jul 19 '17 at 23:24
  • epgsCallsignNMD: WADL epgsProgramIdNMD: EP003954600352 epgsStartLongNMD: 1501387200000 – JavaNoob Jul 19 '17 at 23:24
  • as seen in the console. – JavaNoob Jul 19 '17 at 23:25
  • This is the second record in the same file and then the error appears right after it in the console. epgsCallsignNMD: XXXX epgsProgramIdNMD: EP0001691600XX epgsStartLongNMD: 1501385400000 – JavaNoob Jul 19 '17 at 23:26
  • According to [this](https://stackoverflow.com/questions/32943897/java-ioexception-stream-closed) and [this](https://stackoverflow.com/questions/22611092/error-message-stream-closed) and looking at your code... I think you should put this line `readXMP.close();` outside of the both `do-while` loops – Dazak Jul 19 '17 at 23:33
  • I figured it out...I think...I do a readXMP.close right after the inner do/while loop. and that's inside the outer loop where it then wants to read that same file again. I moved the close to outside the outer loop. – JavaNoob Jul 19 '17 at 23:34
  • LOL...your post came through just as I wrote mine. – JavaNoob Jul 19 '17 at 23:35
  • Thanks for the feedback – JavaNoob Jul 19 '17 at 23:35