2012-11-07 65 views
0

我认为smallIndexindextemp都有值,所以林不知道为什么我得到的错误。任何人都可以向我解释为什么发生这种情况?该错误信息是:我不知道如何解决错误NullPointerException

异常线程“main”显示java.lang.NullPointerException

public class LinkedList 
{ 
    public class LinkedListNode 
    { 
    public int info; 
    public LinkedListNode next; 
    public LinkedListNode back; 

    public LinkedListNode() 
    { 
     info = 0; 
     next = null; 
     back = null; 
    } 
    public LinkedListNode(int item) 
    { 
     info = item; 
     next = null; 
     back = null; 
    } 
    public void displayInfo() 
    { 
     System.out.print(info + " "); 
    } 
    } 
    protected int count; 
    protected LinkedListNode first; 
    protected LinkedListNode last; 

    public LinkedList() 
    { 
    first = null; 
    last = null; 
    count = 0; 
    } 
    public void initializeList() 
    { 
    first = null; 
    last = null; 
    count = 0; 
    } 
    public boolean isEmpty() 
    { 
    return (first == null); 
    } 
    public int length() 
    { 
    return count; 
    } 
    public void print() 
    { 
    LinkedListNode current = first; 
    while (current != null) 
    { 
     current.displayInfo(); 
     current = current.next; 
    } 
    } 
    public void insertNode(int insertItem) 
    { 
    LinkedListNode newNode = new LinkedListNode(insertItem); 
    if (isEmpty()) 
    { 
     first = newNode; 
     last = newNode; 
     count++; 
    } 
    else 
    { 
     last.next = newNode; 
     newNode.back = last; 
    } 
    last = newNode; 
    } 
    public LinkedListNode partition(LinkedList list, 
    LinkedListNode first, LinkedListNode last) 
    { 
    LinkedListNode smallIndex = first; 
    LinkedListNode index = smallIndex.next; 
    LinkedListNode temp = new LinkedListNode(); 
    int pivot = first.info; 

    while (index != last.next) 
    { 
     if((index.info) < pivot) 
     { 
      smallIndex = smallIndex.next; 
      temp.info = index.info; 
      index.info = smallIndex.info; 
      smallIndex.info = temp.info; 
     } 
     index = index.next; 
    } 
    temp.info = first.info; 
    first.info = smallIndex.info; 
    smallIndex.info = temp.info; 
    System.out.print("The list after QuickSort is: "); 
    list.print(); 
    System.out.print("\n"); 
    return smallIndex; 
    } 
    public void recQuickSort(LinkedList list, LinkedListNode first, 
    LinkedListNode last) 
    { 
    while(first != last) 
    { 
     LinkedListNode pivotLocation = partition(list, first, last); 
     recQuickSort(list, first, pivotLocation.back); 
     recQuickSort(list, pivotLocation.next, last); 
    } 
    } 
    public void quickSortLinkedList(LinkedList list) 
    { 
    recQuickSort(list, list.first, list.last); 
    } 

} 



import java.util.*; 

public class testLinkedListQuickSort 
{ 
    static Scanner console = new Scanner(System.in); 

    public static void main(String[] args) 
    { 
    LinkedList linkedlist = new LinkedList(); 
    int num; 

    System.out.println("Enter numbers to add to linked list:"); 
    num = console.nextInt(); 
    while (num != 0) 
    { 
     linkedlist.insertNode(num); 
     num = console.nextInt(); 
    } 
    linkedlist.quickSortLinkedList(linkedlist); 
    linkedlist.print(); 
    } 
} 
+6

发布堆栈跟踪。 – Abubakkar

+1

以及您运行的确切代码 - 按哪个顺序调用哪些方法。 – ApproachingDarknessFish

+1

你在哪个行中得到异常? – Suranga

回答

0

在你partition()方法,你没有做多少检查null值。例如,如果smallIndexindextemplastnull,那么这将炸毁。还有first,如果是null,会导致NPE。

确保您在递归调用中传递的内容不是空节点是个好主意。我没有看到很多检查。

1

关于你的评论:与您通话

recQuickSort(list, first, pivotLocation.back); 

分区做,如果pivotLocation.backnull则分区方法被调用last == null导致您的NPE。

0

NullPointerException发生在您尝试访问没有值的参考时。

或者调用空对象的实例方法并访问或修改空对象的字段。

或者当您将null传递给期望实际值的方法时。它意味着说空对象被非法使用。

我没有看你的代码很多,但要解决这个问题,确定哪个对象实例为null并导致问题。您需要修改代码才能添加适当的空检查验证

相关问题