As the title says, I am getting a NullPointerException at list[i].setName(name);
and I can't figure out why. A Student contains a surname and a score. data.txt is in a format of first line: amount of entries
second line: name (space) score, same for third, fourth etc.
New at Java and on Stack Overflow, so please let me know what other details I should give. The method in question is below:
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;
}