获取链表空指针,我不明白为什么

时间:2013-05-06 21:35:53

标签: java linked-list

该程序应该允许用户将整数插入到链接列表中并始终对它们进行排序。我坚持为什么在我将值返回到我的其他方法后,此时我得到一个空指针异常。感觉就像我现在在圈子里。我有虚拟打印语句试图找出问题。

班级节点:

public class Node {
  Comparable data;
  Node next;

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

Class 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);
    }
  }
}

Class 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类中的实际insert方法中正确打印,但在类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)

3 个答案:

答案 0 :(得分:1)

head = sll.insert(head, insertItem);

而不是

sll.insert(head, insertItem);

答案 1 :(得分: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()

的递归调用

答案 2 :(得分: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为null,因此会出现NullPointerException。