【发布时间】:2015-10-03 11:23:18
【问题描述】:
正如标题所说,我在list[i].setName(name); 收到 NullPointerException,但我不知道为什么。学生包含姓氏和分数。 data.txt 的格式为第一行:条目数量
第二行:名称(空格)分数,第三,第四等相同。
Java 和 Stack Overflow 上的新手,所以请让我知道我应该提供哪些其他细节。有问题的方法如下:
public static Student [] readListFromFile() {
Scanner s = new Scanner("data.txt");
File fileName;
boolean weGood = false;
while (weGood == false) {
System.out.println("Please enter the file name:");
fileName = new File(getInput()); //user can input their own filename
try {
s = new Scanner(fileName);
weGood = true;
} catch (FileNotFoundException e) {
System.out.println("File not found, please try again");
}
}
int listlength = Integer.parseInt(s.nextLine());
Student [] list = new Student[listlength];
for (int i = 0; i < list.length && s.hasNext() == true; i++) {
String name = s.next();
Double score = Double.parseDouble(s.next());
list[i].setName(name);
list[i].setScore(score);
}
return list;
}
【问题讨论】:
-
list是一个listlength空值数组。调试会告诉你。
标签: java arrays eclipse nullpointerexception