0

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.

2
  • 1
    Has gradeList been initialized? Commented Mar 24, 2014 at 13:18
  • where do you initiate private ArrayList gradeList;??? Commented Mar 24, 2014 at 13:19

5 Answers 5

5

You are never instanitating the arraylist. Try

private ArrayList<Integer> gradeList = new ArrayList<Integer>();
Sign up to request clarification or add additional context in comments.

4 Comments

This is very likely the reason, but private ArrayList<Integer> gradeList; was added to the OP's code by an edit from RKC...
OK - I'll update my answer to reflect the changes to the OP's question
I'm an idiot. I initialized the ArrayList in my constructor it fixed it. But I am getting a different error now, an "OutOfMemoryError" that still points to the same line. Is something wrong with my loop or scanner?
Your while(true) loop looks suspicious to me. You are reading the input from the scanner and then adding it forever in to the arraylist
2

gradeList must be null. Make sure you initialize it before using it.

It can be initialized like this:

private ArrayList<Integer> gradeList = new ArrayList<Integer>();

Comments

1

It seems like you're not initializing gradeList.

Either initialize it inline:

private ArrayList<Integer> gradeList = new ArrayList<Integer>();

Or in a constructor:

public MyClass() {
    gradeList = new ArrayList<Integer>();
    // ...
}

Comments

1

Make sure your ArrayList is initialized.

For instance with an instance field gradeList as ArrayList<Integer>, use:

ArrayList<Integer> gradeList = new ArrayList<Integer>();

... before referencing it.

Comments

0

you should must add the ArrayList is initialized. and

you should try this following as:

private List<Integer> gradeList = new ArrayList<Integer>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.