Hi guys,
I am having a problem with my linkedlist class. I am making it from scratch and I want the removeAll method to remove all occurences of item from the linked list. If item is not present, I want the method to quit without throwing.
It seems simple since I already made a remove method:
//removes the first occurance of an object from the list. public void remove(Object data){ //nothing to remove if nothing is in the list if(isEmpty()){ return; } ListNode current=front; //handles if first element is the object we are looking for.. if(current.getValue().equals(data)){ removeFirst(); return; } //handles if non-first element is object we are looking for while(!current.getNext().getValue().equals(data)){ current=current.getNext(); } current.setNext(current.getNext().getNext()); }
The above method works...however when I try to make my removeAll:
public void removeAll(Object data){ //nothing to remove if nothing is in the list if(isEmpty()){ return; } ListNode current=front; while(current!=null){ if(current.getValue().equals(data)){ removeFirst(); } else if(current.getNext().getValue().equals(data)){ current.setNext(current.getNext().getNext()); } else{ current=current.getNext(); } } }
It always casts a nullpointerexception. I've tried for a couple days now and really I can't figure out what is wrong..I've printed out all the variables and nothing seems to be a problem...do I need to return or something? The loop seems to be running infinitely..
thanks,
-dan