开发者

Sorting a linked list

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜