2013-05-06 76 views
0

該程序應該允許用戶將整數插入鏈接列表並保持它們始終排序。我被困在爲什麼我得到一個空指針異常在這一點後,我的價值回到我的另一種方法。感覺就像我現在在圈子裏一樣。我有虛擬打印語句來嘗試找出問題。獲取鏈接列表空指針,我不明白爲什麼

類節點:

public class Node { 
    Comparable data; 
    Node next; 

    Node(Node n, Comparable a) { 
    this.data = a; 
    this.next = n; 
    } 
} 

類SortedLinkedList:

public class SortedLinkedList { 
    Node head = null; 
    private Comparable SortedLinkedList; 


    public SortedLinkedList() { 
    this.head = null; 
    this.SortedLinkedList = SortedLinkedList ; 
    } 

    public Node insert(Node head, Comparable a){ 
     if (head == null || (head.data.compareTo(a)> 0)) 
     { 
      System.out.println("In insert first if"); 
      head = new Node(head, a); 
      //head = new Node(head, 22); 
      System.out.println("Head = " + head.data + " before  return"); 
      return head; 
     } 
     Node pointer = head; 
      while (pointer.next != null) 
      { 
       if (pointer.next.data.compareTo(a) > 0){ 
        System.out.println("In insert, in while, in if"); 
        break; 
       } 
       pointer = pointer.next; 
      } 
     return head; 
    } 

    public void print(Node head){ 
    System.out.println("In print outside of for" + head); 
    for (Node pointer = head; pointer != null ; pointer = pointer.next) 
    { 
     System.out.println("In print"); 
     System.out.print(pointer.data); 
    } 
    } 
} 

類TestInteger

public class TestInteger implements Comparable{ 
    // This is the User Interface for manipulating the List 

    static SortedLinkedList sll = new SortedLinkedList(); 

    public static void nodeMenu() { 
     Node head = sll.head; 
     System.out.println(head); 
     int option; 

     while(true){ 
      System.out.println(); 
      System.out.println("**** Integer Node Menu ****"); 
      System.out.println("****************************"); 
      System.out.println("** 1. Insert    **"); 
      System.out.println("** 2. Delete    **"); 
      System.out.println("** 3. Clear    **"); 
      System.out.println("** 4. Smallest   **"); 
      System.out.println("** 5. Largest    **"); 
      System.out.println("** 6. Return to Main Menu **"); 
      System.out.println("****************************"); 

      Scanner sc = new Scanner(System.in); 
     try{ 

      option = sc.nextInt(); 
      switch (option){ 
      case 1:{ 
       try{ 
        System.out.println("Type an integer to insert: "); 
        int x = sc.nextInt(); 
        Integer insertItem = new Integer(x); 
        sll.insert(head, insertItem); 

        System.out.println("After insert back in case1 head = " + head.data); 
        sll.print(head); 
       }catch(InputMismatchException e){ 
        System.out.println("Enter only integers"); 
        sc.nextLine(); 
       } 
       nodeMenu(); 
      } 

它正確地打印在類SortedLinkedList內的實際插入方法而獲得一個空指針在類TestInteger。下面是輸出:的

1 
Type an integer to insert: 
5 
In insert first if 
Head = 5 before return 
Exception in thread "main" java.lang.NullPointerException 
at CS_240_HW_2.TestInteger.nodeMenu(TestInteger.java:58) 
at CS_240_HW_2.Main.mainMenu(Main.java:52) 
at CS_240_HW_2.Main.main(Main.java:30) 

回答

0

你的代碼有很多需要改變的地方。例如,你只需要一個頭,你只需要一個Node類的數據。 SortedLinkedList類可以是一種實用工具類,只有一些方法用於以特定方式欺騙節點。

所以我建議改爲Node。這個類包含所有的數據,除了頭部本身。

public class Node { 
    Comparable data; 
    Node next; 

    Node(Comparable a) { 
    this.data = a; 
    } 
} 

然後對插入器類進行這些更改。這個類只是一些有用的方法,用於在你的鏈表和/或它的節點上做些簡單的事情。

public class SortedLinkedList { 

    public Node insert(Node head, Comparable a){ 

     Node curr = head; 
     Node prev = null; 
     while (curr != null && curr.data.compareTo(a) > 0) { 
      prev = curr; 
      curr = curr.next; 
     } 
     if (prev = null) { 
      return new Node(a); 
     } 
     prev.next = new Node(a); 
     prev.next.next = curr; 
     return head; 
    } 

    // print doesn't need changing 
} 

而對於測試類沒有太多的事情要改變:

public class TestInteger implements Comparable{ 
    // This is the User Interface for manipulating the List 

    static SortedLinkedList sll = new SortedLinkedList(); 
    Node head = null; 

    public static void nodeMenu() { 

     // no changes in this part ... 

        Integer insertItem = new Integer(x); 
        head = sll.insert(head, insertItem); 
       }catch(InputMismatchException e){ 
        System.out.println("Enter only integers"); 
        sc.nextLine(); 
       } 
      } 

一定要拿出遞歸調用nodeMenu()

1
head = sll.insert(head, insertItem); 

代替

sll.insert(head, insertItem); 

+0

哇謝謝你我真不敢相信我錯過了! – Neophyte 2013-05-06 21:56:14

0

初始化列表:

static SortedLinkedList sll = new SortedLinkedList(); 

在此構造方法中,列表的頭部設置爲null:

this.head = null; 

然後你一個變量初始化成列表的頭:

Node head = sll.head; 

所以head爲空。

然後嘗試打印head.data值:

System.out.println("After insert back in case1 head = " + head.data); 

而且由於head爲空,你會得到一個NullPointerException。