0

、、およびすべてに値があると思うのでsmallIndex、なぜエラーが発生するのかわかりません。なぜこれが起こっているのか誰かが私に説明できますか?エラーメッセージは次のとおりです。indextemp

スレッド「メイン」の例外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();
  }
}
4

3 に答える 3

1

あなたのコメントについて:パーティションを呼び出す

recQuickSort(list, first, pivotLocation.back);

場合pivotLocation.backは、NPE につながるnullパーティション メソッドが呼び出されます。last == null

于 2012-11-07T05:42:09.407 に答える
0

NullPointerException値のない参照にアクセスしようとすると発生します。

または、null オブジェクトのインスタンス メソッドを呼び出して、null オブジェクトのフィールドにアクセスまたは変更する場合。

または、実際の値を期待するメソッドに null を渡す場合。null オブジェクトが不正に使用されているということです。

私はあなたのコードをあまり見ていませんでしたが、これを解決するには、どの Object インスタンスが null であり、問​​題を引き起こしているかを特定します。適切な null チェック検証を追加するには、コードを変更する必要があります

于 2012-11-07T05:49:35.383 に答える
0

あなたの方法では、値partition()をあまりチェックしていません。nullたとえば、smallIndexindextemp、またはでlastある場合null、それは爆発します。もありfirst、それが の場合null、NPE が発生します。

再帰呼び出しで渡されるものが空のノードでないことを確認することをお勧めします。そのためのチェックはあまり見ません。

于 2012-11-07T05:38:44.213 に答える