1

I am following the ebook Big Java Late Objects - and I have problem resolving on of the tasks. It is about inheritance. There is Question class and one sub ChoiceQuestion class that extends it.

But when I run the code in my testclass it calls the null exception and I don't know why.

Exception in thread "main" java.lang.NullPointerException
at ChoiceQuestion.addChoice(ChoiceQuestion.java:12)
at TestClass.main(TestClass.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at                  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Here are my classes:

public class Question {
private String text;
private String answer;

/**
 *  Constructs a queston with empty question and answer.
 */
public Question(){
    text = "";
    answer = "";
}
//=============Setter methods============
public void setText(String questionText){
    text = questionText;
}

public void setAnswer(String correctResponse){
    answer = correctResponse;
}
//==============Getter Methods=============
public boolean checkAnswer(String response){
    return answer.equals(response);
}
public void display(){
    System.out.println(text);
}
}

And here is my subclass:

class ChoiceQuestion extends Question {
private ArrayList<String> choices;


//=========Methods==============
public void addChoice(String choice,boolean correct){
    choices.add(choice);
    if(correct){
        //convert choices.size() to string
        String choiceString = "" + choices.size();
       this.setAnswer(choiceString);
    }
}

public void display(){
    // Display the question text
    super.display();
    // Display the answer choices
    for(int i=0;i<choices.size();i++){
        int choiceNumber = i + 1;
        System.out.println(choiceNumber + ": " + choices.get(i));
    }
}
}

And my test Class:

import java.util.Scanner;

public class TestClass {
public static void presentQuestion(Question q){
    q.display();
    System.out.println("Your answer: ");
    Scanner in = new Scanner(System.in);
    String response = in.nextLine();
    System.out.println(q.checkAnswer(response));
}
public static void main(String[] args) {
    Question first = new Question();
    first.setText("James Gosling");
    first.setAnswer("James Gosling");

    ChoiceQuestion second = new ChoiceQuestion();
    second.setText("In which country was the inventor of Java born? ");
    second.addChoice("Austria", false);
    second.addChoice("Canada",true);
    second.addChoice("Denmark",false);
    second.addChoice("United States",false); 
    presentQuestion(first);
    presentQuestion(second);

}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • It seems that you havn't create `choices` instance – Dmitry Bychenko May 30 '14 at 11:21
  • choices is null. where have you created an object for the Arraylist . you are doing choices.add(choice) which is nothing but null.add(choice) and so a NPE. First create ArrayList choices object before adding – vikeng21 May 30 '14 at 11:26

4 Answers4

2

Object choices is never initialized.

In Java, objects must be initialized before you can use them. That's why an attempt to call choices.add(choice) yields such an exception.

It is often a good idea to initialize member objects in constructors.

In this case, it looks like you should write a constructor for ChoiceQuestion and initialize your choices ArrayList there.

class ChoiceQuestion extends Question {
    private ArrayList<String> choices;

    ChoiceQuestion(){
        choices = new ArrayList<>();
    }

   //...
cangrejo
  • 2,189
  • 3
  • 24
  • 31
1

Write private ArrayList choices = new ArrayList();

In the class when you declared it.

Sunny
  • 308
  • 2
  • 14
1

You need to initialize your list before use:

private List<String> choices = new ArrayList<String>();
Sanjeev
  • 9,876
  • 2
  • 22
  • 33
1

The choices member is never initialized, so the first time you try to access it, you fail with a NullPointerException. You could, for example, initialize it in the class definition:

class ChoiceQuestion extends Question {
    private ArrayList<String> choices = new ArrayList<>();
    // Rest of the class
Mureinik
  • 297,002
  • 52
  • 306
  • 350