2

I am having a really frustrating issue:

I am trying to run an iterator, but it keeps on coming up with java.lang.NullPointerException at the hasNext class.

I am not quite sure where it might be trying to use a null value. I am assuming it is something to do with current. I added a if statement to check if current is null. But then it returns and unexpected value.

Help appreciated.

Code below:

private class Iterator implements Iterator
{
    private Link<T> current;

    public boolean hasNext () { 
        if(current.next == null)
            return false;
        return true;
    }

    public T next() throws OutOfBounds
    {
        if (this.hasNext())
        {
            T element = current.element;
            current = current.next;
            return element;
        }
        else 
            throw new OutOfBounds("No next element to call");
    }
}

private class Link<T> 
{
    private T       element;
    private int     priority;
    private Link<T> next;

    public Link(T t, int p, Link<T> n) 
    {
        this.element = t;
        this.priority = p;
        this.next = n;
    }
}

}

1
  • 3
    Please post a stacktrace. Commented May 17, 2013 at 7:15

3 Answers 3

5

You are probably not initializing current, so your check in the method hasNext should compare for null against currnet before checking against current.next

Modify your check

if(current.next == null)

to:

if(current == null || current.next == null)

Or modify your method as:

public boolean hasNext () { 
   return (current != null && current.next != null);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Even better: return !(current == null || current.next == null)
@heikkim, yes, simpler and more readable. return (current != null && current.next != null);
1

Try to update your hasNext as below to find the issue:

public boolean hasNext () { 
        if(current == null) {
           System.out.println("current is null");
           return false;
        } else if(current.next == null)
            return false;
        }
        return true;
    }

Comments

1

You may use iterator.next() two times inside your while block. Make new object with iterator.next() then use it.

This is the correct way to use it

ArrayList<String> demo = new ArrayList<>();

demo.add("A");
demo.add("B");
demo.add("C");
demo.add("D");

System.out.println(demo);

//Get iterator
Iterator<String> iterator = demo.iterator();

//Iterate over all elements
while(iterator.hasNext()){
/* if you want to use the elemet two times then put in a varialbe and use it.*/
    //Get current element
    String value = iterator.next();
     System.out.println("fist time using"+ value)
    System.out.println( "second time using " + value );

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.