title: java FileReader, Read text Line by Line
body: <p>I have two classes. First, the class TEXT: Here I read a text file with 6 lines. I only want to read line by line, but this works. but I want to start from the third line and also skipo the last one, I only want that the lines which start with 
  
  <p>Here is textfile code.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
  &lt;ICONS ERROR="false" USERNAME="WAZ" FORMAT="FLAT" RECORDS="3"&gt;
  &lt;icon ID="55"  NAM="A" /&gt;
  &lt;icon ID="87"  NAM="B" /&gt;
  &lt;icon ID="53"  NAM="C" /&gt;
  &lt;/ICONS&gt;
  </code></pre>
  
  <p>and here is the code from the filereader:</p>
  
  <pre><code>    package packagechain;
  
  import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.Iterator;
  import java.util.stream.Stream;
  
     public class Text {
  
       String fileName;
       FileReader fr;
       BufferedReader in;
       Stream&lt;String&gt; lines;
       Iterator&lt;String&gt; l;
       boolean hasLine;
  
      public Text() throws FileNotFoundException{
           fileName = "E:/test30.xml";
           fr = new FileReader(fileName);
           in = new BufferedReader(fr);   
           lines = in.lines();
           l = lines.iterator();
           hasLine = true;
      }
  
      public String nextline() {
          String nl;
  
          if(l.hasNext()) {
              nl = l.next();
              //System.out.println(""+nl);
  
          }
          else {
              System.out.println("No new line!");
              hasLine = false;
              nl=null;
          }
          return nl;
      }
      }
  </code></pre>
  
  <p>and here is the code where i can edit my string which i want from the textfile , i use "substring" and this works. but if it comes to the last line where it is no value at the specific substring it comes to an error.... </p>
  
  <p>Error if i delete line 1 and 2 and the alst line in my textfile:
  "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
      at java.lang.String.substring(String.java:1963)
      at packagepackagechain.test4.main(test4.java:18)"</p>
  
  <p>Error if I add line 1 and two and the last one in my textfile
  
  
  ..</p>
  
  <p>Error: 
  Exception in thread "main" java.lang.NullPointerException
      at packagepackagechain.test4.main(test4.java:16)</p>
  
  <pre><code>and here is the code:
  
   package packagechain;
  
  import java.io.FileNotFoundException;
  
  public class test4 {
  
      public static void main(String[] args) {
           Text m;
            String s;
  
            try {
               m = new Text();
               while(m.hasLine) {
                  s = m.nextline();
  
                  String r = s.substring(10,12);
  
                  System.out.println(r);
  
               }
            } catch (FileNotFoundException e) {
               System.out.println("File not found!");
          }
  
  
      }
  }   
  </code></pre>