I'm studying the concepts of pointers. The following list has hypothetical pointers, one pointing to the previous number, and another one, to the next number. And, there's also an element pointing to the current number. When I run the code, the console returns "NullPointerException". The logic behind the code seems okay to me. I'm a beginner and I'd really appreciate some help. I tried to comment the code as much as I could so it can be clearer to understand. Thank you.
aux = start; //aux points to the start of the list. both of them are of the type 'List'
int prev = start.num; //prev points to the number on the start position. both of them are of the type 'int'
while (aux.next != null) { //aux.next means the pointer to the next element in the list
if (aux.num >= aux.next.num) { //if the current number is greater than the next number, or equal
prev = aux.next.num; //prev recieves the lower number
aux.next.num = aux.num; //the greater number is put after the lower number
aux.num = prev; //the lower number is put before the greater number
}
aux = aux.next; //the current element takes a step to the next element so I can progress through the list
aux.num = aux.next.num;
}
Edit: I forgot to say that this list has to be ordered ascending. It also has to be ordered descending on another method.