I'm trying to write a method that will sort objects of a linked list based on an integer variable (named priority) of each object. I've used bubble sort before with arraylists and had no trouble, but for some reason when I try it with a linked list, it gives me a NullPointerException and I can't figure out what I'm doing wrong. Any advice?
public void sortList() {
boolean flag = true;
Item temp = null;
Item position = head;
Item positionLink = position.link;
while(flag) {
flag = false;
while (position != null) {
if(position.getPriority() > positionLink.getPriority()) {
temp.setItem(position);
position.setItem(positionLink);
positionLink.link.setItem(temp);
flag = true;
position = position.getItem();
}
}
}
}