Jin Hoon Jeffrey Bang
Jin Hoon Jeffrey Bang

Reputation: 601

Java iterator: hasNext() returns true but next() is null?

In the following code, tupleIterator.hasNext() returns true but tupleIterator.next() is null.

if (this.tupleIterator != null) {
    if (this.tupleIterator.hasNext()) {
        tuple = this.tupleIterator.next();
        return tuple;
    }
}

this.tupleIterator was derived by calling iterator() method on ArrayList.

Upvotes: 0

Views: 2963

Answers (2)

peeyush pathak
peeyush pathak

Reputation: 3663

You must have added null in your arraylist so if you do not want null values in the lust you can do this by either checking before adding value to the list or you can extend Arraylist class and override

boolean add(E e)
void add (int index, E e)
boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)

Upvotes: 1

Huskell
Huskell

Reputation: 323

Seems your Interator has values, but they were inserted as null. Without the code before this part is hard to say.

Upvotes: 1

Related Questions