This is my code:
private ArrayList<Integer> gradeList;
public void addGrades(Scanner reader) {
int grade = reader.nextInt();
if (grade != -1) {
this.gradeList.add(grade); // --> NullPointerException
}
}
I can't figure out why I am getting a NullPointerException, because I have made methods like this often without this problem. I thought changing it to this:
public void addGrades(Scanner reader) {
int grade = Integer.parseInt(reader.nextLine());
while (true) {
if (grade == -1) {
break;
} else {
this.gradeList.add(grade); // --> NullPointerException
}
}
}
would help, but it did not fix the problem. In both cases, the problem is on the line this.gradeList.add(grade). I also tried declaring the ArrayList to be null within the method before the loop, but realized that was redundant. So I'm not sure what I'm doing wrong.
gradeListbeen initialized?