title: Java I/O File Not Found
body: <p>Currently trying to write a program to take input from a file and store it in an array. However, whenever I try to run the program the file cannot be found (despite file.exists() and file.canRead() returning true).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    public void getData (String fileName) throws FileNotFoundException
  {
      File file = new File (fileName);
      System.out.println(file.exists());
      System.out.println(file.canRead());
      System.out.println(file.getPath());
      Scanner fileScanner = new Scanner (new FileReader (file));
      int entryCount = 0;     // Store number of entries in file
  
      // Count number of entries in file
      while (fileScanner.nextLine() != null)
      {
          entryCount++;
      }
  
      dirArray = new Entry[entryCount];   //Create array large enough for entries
      System.out.println(entryCount);
  
  }
  
  public static void main(String[] args)
  {
      ArrayDirectory testDirectory = new ArrayDirectory();
  
  
      try
      {
          testDirectory.getData("c://example.txt");
      }
  
      catch (Exception ex)
      {
          ex.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>(In it's current state the method is only designed to count the number of lines and create the array)</p>
  
  <p>The console output is as follows: true true c:/example.txt</p>
  
  <p>The program seems to throw a 'FileNotFoundException' on the line where the scanner is instantiated.</p>
  
  <p>One thing I have noticed when checking the 'file' object when debugging is although it's 'path' variable has the value "c:\example.txt", it's 'filePath' value is null. Not sure if this is relevant to the issue or not</p>
  
  <p>EDIT: After Brendan Long's answer I have updated the 'catch' block. The stack trace reads as follows:</p>
  
  <pre><code>java.util.NoSuchElementException: No line found
      at java.util.Scanner.nextLine(Unknown Source)
      at assignment2.ArrayDirectory.getData(ArrayDirectory.java:138)
      at assignment2.ArrayDirectory.main(ArrayDirectory.java:193)
  </code></pre>
  
  <p>Seemingly the scanner doesn't recognize the file and thus can't find the line</p>