Constructor is not initializing a Scanner object
<p>In the following code the constructor is not initializing the numFile Scanner. I added the contents of the constructor to the main method to make it work. If I do not do this a java.lang.NullPointerException is thrown. Would someone mind explaining why? Also, do I need to throw an IOException on the constructor?</p>

<p>Thanks for any helpful advice.</p>

<p>Tony</p>

<pre><code>import java.io.*;
import java.util.Scanner;

public class CountPositiveIntegers {

    static Scanner numFile;
    static String fileName;     // the name of the file in which the integers are stored
    static int number;          // holds the current number being read
    static int counter;         // a counter used to sum the number of positive integers


    public CountPositiveIntegers() throws IOException { 
        fileName ="D:\\Java\\Source\\numFile.dat"; 
        System.out.println("File Name: " + fileName);   
        numFile = new Scanner(new FileReader(fileName)); 
        number = 0; 
        counter = 0; 
    }


    public static void main(String[] args) throws FileNotFoundException {
        // numFile is not being initializing in the constructor
        fileName = "D:\\Java\\Source\\numFile.dat";
        numFile = new Scanner(new FileReader(fileName));
        number = 0;
        counter = 0;

        if (numFile.hasNext()) {                    // check to see if there are any values in the file
            while (numFile.hasNextInt()) {      // reads in integers
                number = numFile.nextInt();
                if (number % 2 == 0 &amp; number != 0) {
                    counter++;                  
                }
            }
            numFile.close();                            // close the file           

            // print to screen the number of even integers stored in the file
            System.out.println("There are " + counter
                    + " even numbers in this file");
        } else {
            System.out.println("The file is empty.");
        }   

        System.exit(0); // cleanly exit the program
    }
}
</code></pre>
