title: Trying to find the nth element to the last from a list (with a file input)
body: <p>Input would look like </p>
  
  <pre><code>a b c d 4
  e f g h 2
  </code></pre>
  
  <p>where each line would be read like a list and integer representing as an index in the list</p>
  
  <p>I first try to read the file line be line and store it in the list. Heres what i have </p>
  
  <pre><code>public class FileReader {
  
      public static void main(String[] args) {
          String line = null;
          List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
          try {
              FileInputStream fstream = new FileInputStream("test.txt");
              DataInputStream in = new DataInputStream(fstream);
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              // File file = new File("test.txt");
              // Scanner scanner = new Scanner(file);
              while ((line = br.readLine()) != null) {
                  list.add(line);
              }
  
              System.out.println(list);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>Now i want to remove the white spaces from the list and store the values in char array and then i was planning on traversing that array backwards till the nth element, depending on the input for n. </p>
  
  <pre><code>String[] elements = line.trim().split("\\s");
  char[] chars = new char[elements.length - 1];
  int i= Integer.parseInt(elements[elements.length - 1]);
  for (i = 0; i &lt; elements.length - 1; i++)
      char[i] = elements[i].charAt(i);
  </code></pre>
  
  <p>Someone provided me this piece of code earlier and i tried it and it throws a nullpointerexception at String[] elements. </p>