6 Replies Latest reply: Mar 22, 2007 10:53 AM by 807606 RSS

    readLine with Standard Input from File produces NullPointer

    807606
      Hello all, I'm trying to create something that will read lines from standard input via a file. It works if I manually enter each line via standard in. However, it returns a NullPointerException when invoked like this:

      java fileReader < standardInputLines.txt

      StandardInputLines.txt will always have 3 lines. It looks something like this.:

      Line1Content
      Line2Content
      Line3Content

      The first thing I do is look for the Line1Content. After that I returned a parsed element, call some other methods. Here's what it looks like:
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      boolean done = false;
      String xml = "";
                
      while( !done )
       {
      String line = in.readLine();
      xml = line;            
      done = xml.length() > 0;
      }
      The parsed XML object is then sent to other methods. After a few of them are executed, the same method is called again. Clearly readLine should return Line2Content. However, it returns null? Anyone have an ideas?
        • 1. Re: readLine with Standard Input from File produces NullPointer
          807606
          the end of the file will be read in as null. a better way to do this is to have your exit condition as line == null. incidentally, why are you using 2 strings here? one should do it!
          • 2. Re: readLine with Standard Input from File produces NullPointer
            807606
            I actually first thought it meant the end of file, too. But when I debugged it I printed out each time readLine was called. It was only called two times. The first time returned the first line while the second time returned null.

            Checking for != null is a standard procedure for files with arbitrary sizes. But since I know the file will always have 3 lines I don't need the check. I think the problem has something to do with System.in getting messed up after the method returns, calls other methods, then is called again.

            I've inherited this code so I'm looking for a quick simple, fix but it looks like I may have to rewrite it.
            • 3. Re: readLine with Standard Input from File produces NullPointer
              807606
              I actually first thought it meant the end of file,
              too. But when I debugged it I printed out each time
              readLine was called. It was only called two times.
              The first time returned the first line while the
              second time returned null.

              Checking for != null is a standard procedure for
              files with arbitrary sizes. But since I know the file
              will always have 3 lines I don't need the check. I
              think the problem has something to do with System.in
              getting messed up after the method returns, calls
              other methods, then is called again.
              eh? if this is a file that is guaranteed to only ever have 3 lines, why bother looping indefinately until you find a line of zero-length? which you won't get anyway? you're doing a much more fragile and broken check than a simple check for null. seems odd to say the simple fix isn't necessary, since it clearly is. also, most bugs come from assumptions like "I know the file will always have 3 lines so I don't need to check that". trust me
              • 4. Re: readLine with Standard Input from File produces NullPointer
                807606
                I'm showing a simplified version of the code. There are a few more checks but I removed them to make it easier to focus on the problem at hand. The problem is not reading XML messages, parsing incorrectly, etc. This works fine when the contents of the text file are entered manually.

                Also, I can assure you that the file will contain that many lines. This is an academic environment. People have to follow the guidelines or they will fail.
                • 5. Re: readLine with Standard Input from File produces NullPointer
                  807606
                  even if in your environment you can be "sure" there will only be 3 lines, it's a bad habit to get into, that sort of assumption. and it seems odd you "know" this but are not simply looping three times and be done with it. I've told you why your problem has arisen, it's up to you what you do with it
                  • 6. Re: readLine with Standard Input from File produces NullPointer
                    807606
                    I definitely agree it's a bad practice and I'm going to remove it. Thanks for the help.

                    Also, if anyone finds this on Google some day, the problem had to do with the BufferedReader being reinstantiated. The quick fix for this was to simply pass the BufferedReader around and perform the same inner logic.