0

I am trying to implement a simple Quiz Game and when overriding toString method in my Question class I got a NullReferenceException. Where the problem comes from? This is my code in Question class

    public class Question {
        private String text;
        private Answer[] answers;
        private final static int ANSWER_SIZE_ARRAY = 4;

        public Question(String text, Answer[] answers) {
          this.text = text;
          answers = new Answer[ANSWER_SIZE_ARRAY];
          this.answers = answers;
        }

        public void setText(String text) {
        this.text = text;
     }

     public void setAnswers(Answer[] answers) {
          this.answers = answers;
     }

     public String getText() {
         return this.text;
     }

     public Answer[] getAnswers() {
         return this.answers;
     }

    //test toString
    public String toString() {
         String result = this.getText() + "\n";
         for (Answer a : this.answers) {
             result += a.getText() + "\n"; // HERE COMES THE PROBLEM
         }
         return result;
     }

 }

And my main method is like:

    public class MainGameTestMethod {
      public static void main(String[] args) {
        Answer a1 = new Answer("Krisko", true);
        Answer a2 = new Answer("Beatz", false);
        Answer a3 = new Answer("Ivan", false);
        Answer a4 = new Answer("Pesho", false);
        Question q1 = new Question("Whats your name?", new Answer[] { a1, a2, a3, a4 });
        System.out.println(q1.toString());
    }
}
user7460099
  • 53
  • 1
  • 2
  • 9
  • Remove `answers = new Answer[ANSWER_SIZE_ARRAY];` from the ctor. – 001 Mar 21 '17 at 18:43
  • In your constructor you set `this.answers` to an empty array of size 4. That's an array of 4 `null` objects. Then you loop over that array and try to use those `null` objects. – David Mar 21 '17 at 18:44
  • You're overwriting the constructor's parameter ˋanswers = new ...ˋ, you don't need that line. – MikaelF Mar 21 '17 at 18:45

1 Answers1

2

Your constructor does not make any sense.

    public Question(String text, Answer[] answers) {
      this.text = text;
      answers = new Answer[ANSWER_SIZE_ARRAY];
      this.answers = answers;
    }

You are passing an array but you don't use it. You just create a new array. I think you want to use it as a member variable. Therefore you have to remove the second line in your constructor:

    public Question(String text, Answer[] answers) {
      this.text = text;
      this.answers = answers;
    }

Your array answers always has only null values and therefore you get the error.

Edit because of the commentar that the array should have the size 4:

    public Question(String text, Answer[] answers) {
      this.text = text;
      if(answers == null || answer.length != 4){
          //do some exception handling e.g. throw error
         //or do
          //if(answer != null){
// take care that this method could also lead to null values in your new array if the old arrays length is < 4
       //  this.answers = Arrays.copyOf(answers, 4); 
       // }

      }
      else{
          this.answers = answers;
      }
    }
Markus
  • 1,141
  • 1
  • 9
  • 25