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
精彩评论