Help with using iterator remove on double linked list
I am trying to remove data from a double linked list using the remove method using a iterator and I keep getting a null pointer exception.
This is what I have
System.out.print("Enter student ID: ");
String studentID2 = in.next();
Iterator<Student> iter = students.iterator();
while (iter.hasNext()) {
Student stu = (Student) iter.next();
if(stu.getStudentID().equals(studentID2)){
iter.remove();
}
}
Says the error is in my remove method
public void remove() {
lastItemRet = lastItemRet.prev;
lastItemRet.next = nextItem; The error is on this line.
nextItem.prev = lastItemRet;
}
Re: Help with using iterator remove on double linked list
Post an SSCCE and the full error code, or else you get guesses. My guess, nextItem is not defined anywhere or is out of scope of the location you are trying to access it.
Re: Help with using iterator remove on double linked list
The full error I get isThe full error I get is
Exception in thread "main" java.lang.NullPointerException
at DLList$Node.access$3(DLList.java:105)
at DLList$ListIter.remove(DLList.java:182)
at Menu.display(Menu.java:46)
at Prog3.main(Prog3.java:17)
Re: Help with using iterator remove on double linked list
What could be null on that line? Could lastItemRet be null? Add some println's in there to try and debug, or use a debugger to step through the code and see what is null. Without an SSCCE it is hard to comment on a fix.