在以下代码中,构造函数未初始化numFile Scanner。我将构造函数的内容添加到main方法以使其工作。如果我不这样做,则抛出java.lang.NullPointerException。有人会介意解释原因吗?另外,我是否需要在构造函数上抛出IOException?
感谢任何有用的建议。
贝
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 & 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
}
}
答案 0 :(得分:3)
您必须显式调用构造函数才能使其正常工作。 (你永远不会创建new CountPositiveIntegers()
)。
答案 1 :(得分:1)
实际上,您只使用静态变量,不会调用构造函数,并且此类对象没有非静态字段。面向对象编程的一个例子:
public class CountPositiveIntegers {
Scanner numFile;
String fileName; // the name of the file in which the integers are stored
public CountPositiveIntegers(String fname) throws IOException {
fileName = fname;
System.out.println("File Name: " + fileName);
numFile = new Scanner(new FileReader(fileName));
}
public static void main(String[] args) throws FileNotFoundException {
try {
CountPositiveIntegers obj = new CountPositiveIntegers("D:\\Java\\Source\\numFile.dat");
int number = 0; // holds the current number being read
int counter = 0; // a counter used to sum the number of positive integers
if (obj.numFile.hasNext()) { // check to see if there are any values in the file
while (obj.numFile.hasNextInt()) { // reads in integers
number = obj.numFile.nextInt();
if (number % 2 == 0 & number != 0) {
counter++;
}
}
obj.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
} catch (IOException ex) {
Logger.getLogger(CountPositiveIntegers.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 2 :(得分:0)
我认为问题在于,当你的程序启动时,不创建一个执行main
的类的新实例,而只是运行{中的代码{1}}。由于您的初始化代码在构造函数中,因此您从未实际运行,因为您没有创建主类的实例。
要解决此问题,我强烈建议让main
创建main
所在类的新实例,然后对该对象执行所有操作,而不是创建所有内容{{1}并直接在main
执行操作。您当前的方法并不是特别好的设计。
希望这有帮助!
答案 3 :(得分:0)
您的CountPositiveIntegers类对象在哪里。
您需要显式调用构造函数,否则编译器将调用默认构造函数。
new CountPositiveIntegers();
答案 4 :(得分:0)
除非您实际调用它,否则不会调用构造函数。 main方法不会隐式调用构造函数,因为它是static
方法。