开发者

Sorting a linked list

开发者 https://www.devze.com 2023-03-04 12:17 出处:网络
I am开发者_StackOverflow社区 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.

I am开发者_StackOverflow社区 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;
    }
}


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消