title: NullPointerException during File IO
body: <p>This is a simple program to read from a text file, but somehow I am getting the following error when i run it </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NullPointerException</p>
  </blockquote>
  
  <p>Program:</p>
  
  <pre><code>package testenglish;
  
  import java.util.*;
  import java.io.*;
  
  
  public class TestEnglish 
  {
  
      public static void main(String[] args) 
      {
           Scanner myscanner= null;
          try
          {
                  myscanner = new Scanner(new File("file.txt"));
          }
          catch (FileNotFoundException e)
          {
  
          }
  
          while (myscanner.hasNextLine())
          {
  
          }
  
          myscanner.close();
      }
  
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/OkaIm.png" alt="enter image description here"></p>
  
  <p>I don't understand why I am getting a <code>NullPointerException</code> when <code>myscanner</code> has already been initialized.</p>
  
  <p>EDIT: How come I am not reading the text file? It is placed in the same folder as the source code.</p>