0

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.

4
  • 2
    On which line is the NullPointerException thrown? Commented Sep 11, 2014 at 13:40
  • You code is not correct ... please read about one sorting algorithm ... start with bubble sort which is easy then try to implement it with an array... then try to do it with linked list ... We could give you the code but that will not help you learn ... Commented Sep 11, 2014 at 13:40
  • @Petter At "aux.num = aux.next.num;", line 10 of the block of code on my question. Commented Sep 11, 2014 at 13:45
  • before using any reference better check != null & tell exactly on which line npe ? before that line, you can check if(aux != null && aux.next != null) Commented Sep 11, 2014 at 13:56

2 Answers 2

2

It happens in the last two lines of your while loop.

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;

Here you assign aux = aux.next, after that you do aux.num = aux.next.num;. If aux is now the last element of your list, aux.next is null and aux.next.num will throw an NPE.

To prevent this, you could do another check before accessing num:

aux = aux.next;
if(aux.next != null) {
    aux.num = aux.next.num;
}
else {
    break;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Okay, but now it goes on an infinite loop. I added the numbers "10" and "4", and printed out how it sorted. Here it is: 'prev = 10 - aux.num = 10 - aux.next.num = 4; prev = 4 - aux.num = 10 - aux.next.num = 4; prev = 4 - aux.num = 10 - aux.next.num = 10; prev = 4 - aux.num = 4 - aux.next.num = 10; prev = 4 - aux.num = 10 - aux.next.num = 10; prev = 10 - aux.num = 10 - aux.next.num = 10; prev = 10 - aux.num = 10 - aux.next.num = 10; prev = 10 - aux.num = 10 - aux.next.num = 10;'
You could add an else block with a single break; which exits you loop. Since aux.next is null when you reach the else block, it also means you're at the end of you list and you can safely break out of the loop.
Still on infinite loop. Check out the code I added after closing my "if" statement (line 9 of the question's block of code). 'if(aux.next != null) { aux.num = aux.next.num; } else { break;}'
Are you still doing aux = aux.next before the if-check? I edited my answer.
I didn't check that. Now it works, but there's a problem with the "prev" variable. I added the following numbers: 4, 10, 6. When I ordered the list using this sorting method, it returned: 4, 10, 10. I presume it lost the "prev" value.
1

You are checking if aux.next is not null, but You don't check this condition for aux.prox (I believe, prox is a pointer).

edit:

So... look at

aux = aux.next;
aux.num = aux.next.num; 

You have to check if new aux.next is not NULL

2 Comments

I'm sorry, "prox" does not exist, it was my mistake to type it. The "prox" you're talking about is "next". I fixed it on the code.
Yes and I checked it and it gets on an infinite loop.

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.