title: java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1585)
body: <p>I'm prompting for user input over and over again until the user types exit in which case the data object will be set to null and it will leave the loop. For the most part this code works, but sometimes I get a <strong>NoSuchElementException</strong> on the line: <code>reader = new Scanner(System.in);</code>. This tends to happen after a few loops and not the first time through.  If I alternatively declare the reader variable within the scope of the while loop then it will fail on the second loop.</p>
  
  <h2>Mostly Working (Sometimes throws exception)</h2>
  
  <pre><code>int level = 1;  
  Scanner reader;
  String selection = null;
  
  while (true) {
      if (data.done) {
          level--;
      }
  
      data.done = false;
      System.out.println(getSubmenu(level, data));
  
      reader = new Scanner(System.in);
  
      if (level &lt;6) {
          selection = reader.nextLine();
      } else {
          level = 4;
      }
  
      if (validSelection(selection)) {
          level = getLevel(level, selection);
          data = getData(level, data, selection);
      } else {
          System.out.println("Invalid entry");
      }
  
      if (data == null) {
          System.out.println("Level "+ level + "selection " + selection);
          break; // exit command was typed
      }
  }
  reader.close();
  </code></pre>
  
  <h2>Alternate (throws exception on second loop)</h2>
  
  <pre><code>int level = 1;  
  String selection = null;
  
  while (true) {
      if (data.done) {
          level--;
      }
  
      data.done = false;
      System.out.println(getSubmenu(level, data));
  
      Scanner reader = new Scanner(System.in);
  
      if (level &lt;6) {
          selection = reader.nextLine();
      } else {
          level = 4;
      }
  
      if (validSelection(selection)) {
          level = getLevel(level, selection);
          data = getData(level, data, selection);
      } else {
          System.out.println("Invalid entry");
      }
  
      if (data == null) {
          System.out.println("Level "+ level + "selection " + selection);
          break; // exit command was typed
      }
      reader.close();
  }
  </code></pre>
  
  <h2>StackTrace</h2>
  
  <pre><code>java.util.NoSuchElementException: No line found
  at java.util.Scanner.nextLine(Scanner.java:1585)
  at org.functional.utils.Menu.run(ServerScripts.java:61)
  at org.functional.utils.ServerScripts.main(ServerScripts.java:18)
  </code></pre>
  
  <p>What am I missing?</p>