当我运行代码时,打印出消息“null”。但该文件充满了数据,并在正确的位置。为什么我得到这个空的例外?
这是读取文件的类:
public class ScoreBoard {
private ArrayList<QuizTaker> scoreBoard = new ArrayList<QuizTaker>();
public ScoreBoard() {
try {
Scanner reader = new Scanner(new File("Scores.txt"));
while (reader.hasNext()) {
String name = reader.nextLine();
int score = reader.nextInt();
reader.nextLine();
QuizTaker taker = new QuizTaker(name, score);
scoreBoard.add(taker);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
这就是Scores.txt文件:
Edward
5
Jimmy
4
Poke
6
Luke
8
Andy
9