Sam
Sam

Reputation: 23

HashSet NullPointerException error during iterating

This is my main code:

package onlineTest;

import java.util.HashSet;
import java.util.Iterator;

public class TestClass {

  public static void main(String[] args) {
    HashSet<TrueFalseQuestion> newList = new  HashSet<TrueFalseQuestion>(); 
    newList.add(new TrueFalseQuestion(1, 2, "sms", 4, true));
    newList.add(new TrueFalseQuestion(2, 3, "erw", 5, false));
    newList.add(new TrueFalseQuestion(3, 4, "Gray", 6, true));

    Iterator<TrueFalseQuestion> iterator = newList.iterator();
    while(iterator.hasNext()) {
      System.out.println(iterator.next());
      System.out.println(iterator.next().getPoints());
    }
  }
}

And this is my TrueFalseQuestion class:

public class TrueFalseQuestion {

  public int examId;
  public int questionNum;
  public String text;
  public double points;
  public boolean answer;

  public TrueFalseQuestion(int examId, int questionNum, String text,
        double points, boolean answer) {
    this.examId = examId;
    this.questionNum = questionNum;
    this.text = text;
    this.points = points;
    this.answer = answer;
  }

  public int getExamId() {
    return examId;
  }

  public int getQuestionNum() {
    return questionNum;
  }

  public String getQuestionText() {
    return text;
  }

  public boolean getAnswer() {
    return answer;
  }

  public double getPoints() {
    return points;
  }
}

When I run my code, it is able to go through the first iteration and print the points out fine, but then it gives a NullPointerException for the second iteration. The line that cases the error is:

System.out.println(iterator.next().getPoints());

I have no idea what the problem is. I assumed that iterator.next() was the current TrueFalseQuestion object and that each iteration, I could use getPoints().

Upvotes: 2

Views: 972

Answers (2)

thegauravmahawar
thegauravmahawar

Reputation: 2823

It's because you're calling itertor.next() twice inside your loop for every iterator.hasNext().

You should do:

while(iterator.hasNext()) {
  TrueFalseQuestion trueFalseQuestion = iterator.next();
  System.out.println(trueFalseQuestion);
  System.out.println(trueFalseQuestion.getPoints());
}

What happens in you're first iteration is that when you call:

System.out.println(iterator.next());

The first TrueFalseQuestion gets printed, however in the very next statement, when you do:

System.out.println(iterator.next().getPoints());

You're unknowingly printing the points of the second TrueFalseQuestion.

So, in you're second iteration, again when you do:

System.out.println(iterator.next());

The third TrueFalseQuestion is printed, and finally, on the very next line when you do:

System.out.println(iterator.next().getPoints());

There isn't a fourth TrueFalseQuestion and you get a NullPointerException because you are calling getPoints() on a null value.

Upvotes: 3

Piyush Vavaliya
Piyush Vavaliya

Reputation: 5

array value in your program can not found your array so it display null pointer exception notice that your array index start with 0

Upvotes: 0

Related Questions