We started working on lists in class and there was this task to separate numbers from a list without creating a new list. So I used sepNum tag along with first inside the list. However it returns nullPointer and I don't understand why. The nullPointer appears at 2nd line in separateNumbers method. Element first exists and is !null.
public void separateNumbers() {
sepNum = first;
while (sepNum.info < 0 || sepNum.info > 9)
sepNum = sepNum.point;
Element previous = sepNum;
Element current = sepNum.point;
while (current != null) {
if (current.info < 0 || current.info > 9){
previous.point = current.point;
current = current.point;
} else {
previous = current.point;
current = current.point;
}
}
}
Here's Element class:
class Element {
char info;
Element point;
public Element (char ch) {
this.info=ch;
this.point=null;
}
public String toString() {
return info+(point == null ? " " + point : "");
}
}
I've been trying to figure this out for a few hours now. Can you guys please help?