Rich Doran
Rich Doran

Reputation: 1

Sorting a linked list

I am having an issue of a Null pointer exception. As much as i try i can't find any sort of help. If someone has an idea please let me know.

for (cursor = head; cursor != null; cursor = cursor.link) {

    k = addScore(cursor.num);
    for (int i = 1; i <= nodeLength(); i++) {

        cursorAdd = head.link;
        j = addScore(cursorAdd.num);

        if (j > k) {

            cursor.link = cursorAdd.link;
            cursorAdd.link = cursor;
        }
        cursorAdd = cursorAdd.link;
    }
}

Upvotes: 0

Views: 222

Answers (2)

Alanyst
Alanyst

Reputation: 1410

You don't check that cursorAdd != null before using it.

Upvotes: 2

Dilum Ranatunga
Dilum Ranatunga

Reputation: 13374

I think your list has only one element. So

head != null
head.link == null;
cursor = head; // cursor != null; cursor.link == null.
cursorAdd = cursor.link; // == null
addScore(cursorAdd.num) <-- NPE

Upvotes: 0

Related Questions