0

My output is: null null null null null null null null Enter flight number to search:

I am getting data from a .txt file which is then stored in a 2D Array Can you help me with the null situation please?

Here is my code:

import java.io.*;
public class Main {
    public static void main (String args[]) throws IOException {
        String search;
        FileReader fr = null;
        LineNumberReader lnr = null;

        String list[] = new String[160];

        String plane[][] = new String [20][8];//Creating an array in an array
        int p,i;
        try{
            //create new reader
            fr = new FileReader("Data.txt");
            lnr = new LineNumberReader(fr);
            String input = null; 
            for(p=0; p<20; p++)
            for(i=0; i<8; i++){
                input = lnr.readLine();
                if (input != null) {
                    plane[p][i]=input;
                }
            }
            //for(p=0; p<1; p++){
            //  for(i=0; i<8; i++)
            //    System.out.println(plane[p][i]);
            //}
            }catch(Exception e){
             // if any error occurs
             e.printStackTrace();
            }finally{
             // closes the stream and releases system resources
             if(fr!=null)
                fr.close();
             if(lnr!=null)
                lnr.close();
        }

        System.out.println("Enter flight number to search: \t");
        search = Keyboard.readString();
        for(p=0; p<20; p++){
            if(search == plane[p][0]){
               System.out.println("Information about flight: "+plane[p][0]);
               System.out.println("----------------------------------");
               System.out.println("Flight Number: "+plane[p][0]);
               System.out.println("Air Line: "+plane[p][1]);
               System.out.println("To: "+plane[p][2]);
               System.out.println("From: "+plane[p][3]);
               System.out.println("Departure Time: "+plane[p][4]);
               System.out.println("Estimated Time: "+plane[p][5]);
               System.out.println("Status: "+plane[p][6]);
               System.out.println("Distance: "+plane[p][7]);
            }
        }
    }
}
Tex_Dex
  • 3
  • 4
  • My guess is `input` variable is null. Print input variable to cross check. – Ravikumar Nov 19 '16 at 10:35
  • I modified your program just enough that I could run it. I cannot persuade it to print `null`, so I assume your problem is somewhere else. My modifications were: Instead of `Data.txt` I filled in a text file on my computer. I commented the for loop back in that prints `plane[p][i]` to see if it gave any nulls, it didn’t. And I had to comment out the lines after `System.out.println("Enter flight number to search: \t");` because I din’t have the `Keyboard` class that you seem to have. – Ole V.V. Nov 19 '16 at 10:42

1 Answers1

0

I strongly believe this is because your Data.txt file is empty. lnr.readLine() will return

null if the end of the stream has been reached

You try a null check in your code, but it doesn’t really help because your array plane contains null from the creation and will continue to contain nulls until you put something else in there.

It seems that already the first call to lnr.readLine() returns null, which I why I think the reader is at the end of the file from the start, that is, the file is empty.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Actually now the output is: Enter flight number to search: KM514 (Then I enter a plane number) (Then the terminal stops) – Tex_Dex Nov 19 '16 at 15:12
  • I think that the problem is when it comes to the If Statement (the comparing part). Isn't it? – Tex_Dex Nov 19 '16 at 15:16
  • Well spotted! I agree and I think the answer is here: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java#513839) (follow the link). – Ole V.V. Nov 19 '16 at 15:17
  • So how should I write it? – Tex_Dex Nov 19 '16 at 15:39
  • Never mind, solved the problem. Thanks for the reference :) Appreciate it – Tex_Dex Nov 19 '16 at 15:42