1 Reply Latest reply: Sep 20, 2007 2:15 AM by EJP RSS

    NullPointer while reading the File by  java.io.LineNumberReader

    807605
      Hi

      I use a org.apache.struts.upload.FormFile.

      I take the InputStream of the File.

      Then i use java.io.LineNumberReader of InputStream.

      Then i read the file line by line as lineNumberReader.readLine().

      Then i check while(lineNumberReader.readLine()! = null )then i proceed.

      It is work well.

      But at the end of the file that is after all the records are readed I got the NullPointerException.

      After the End of the file is reached the while loop
      while(lineNumberReader.readLine()! = null ) is executed.

      At that time i got the Exception.

      Pls can anyone help me to solve?

      Thank you
        • 1. Re: NullPointer while reading the File by  java.io.LineNumberReader
          EJP
          Then i check while(lineNumberReader.readLine()! = null )then i proceed.
          You can't 'proceed' to do anything useful with code like that. You've just either read a line from the file and thrown it away, or read EOF. So you're going to miss lines of data.

          Your loop should look like this:
          String line;
          while ((line = lineNumberReader.readLine()) != null)
          {
           // ...
          }
          The NPE you're getting is being caused in the part of the code you haven't shown us, which is almost all of it.