HXSP1947
HXSP1947

Reputation: 1351

NullPointerException for object

Thanks in advance for the help.

I have the following constructor:

public Question(String text) {
    this.totalPossibleSelections = 1;
    this.text = text;
    this.currentSelections = new QuestionElement[1];
    this.finalSelections = new QuestionElement[1];
}

and this method in the same class:

public void setCurrentSelection(QuestionElement selection) {
    if (totalPossibleSelections == 1){
        currentSelections[0] = selection;
    }
}

where

protected QuestionElement[] currentSelections;

is one of the parameter of my object Question.

currentSelections is null when I call setCurrentSelection which is causing a NullPointerException. currentSelections shouldn't be null because the method setCurrentSelection can only operate on a Question. I have initialized my Question by calling the constructor in another piece of code. What might be going on here?

EDIT: There are no subclasses of Question. Also the constructor is being called. If it wasn't, totalPossibleSelections would be null.

Upvotes: 0

Views: 109

Answers (1)

Blundell
Blundell

Reputation: 76506

perhaps because QuestionElement[] is protected - is a sub class setting it to null.

Change to protected final QuestionElement[] currentSelections to avoid confusion then it can't be null

Upvotes: 3

Related Questions