Java链表节点打印错误

时间:2016-04-27 18:52:31

标签: java

我的链表类有一些问题。我目前正在尝试按照我给他们的指定顺序打印我最喜欢的乐队,但我要么想出的程序只是打印null或只是错误顺序的乐队名称。我很困惑为什么或我缺少什么。任何帮助,将不胜感激。

我目前得到的输出是

Exception in thread "main" java.lang.NullPointerException
at project2.jacobLinkedList.toString(MetalMasher.java:171)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at project2.MetalMasher.main(MetalMasher.java:44)

文件是

import java.util.Collections;
import java.util.List;
public class MetalMasher {
    public static jacobLinkedList jacobList;
    @SuppressWarnings("unchecked")
    public static <T> void main(String[] args) {
    // this is the default constructor.
    jacobList = new jacobLinkedList();
    // add elements to the list.
    jacobList.add("MegaDeth",1993);
    jacobList.add("Slayer",1992);
    jacobList.add("Scar Symmetry",2002);
    jacobList.add("Gojira",2004);
    jacobList.add("Amon Amarth",1997);
    System.out.println("Print: jacobList:" + jacobList);
    System.out.println(".size():" + jacobList.size());
    System.out.println(".remove(2):" + jacobList.remove(2) + " (element removed)");
    System.out.println("Print again:" + jacobList);
    System.out.println(".remove(1):" + jacobList.remove(1) + " (element removed)");
    System.out.println("Print again:" + jacobList);
    System.out.println(".remove(1):" + jacobList.remove(1) + " (element removed)");
    System.out.println("Print again:" + jacobList);
    }
    }

class jacobLinkedList {
private static int counter;
private Node head;

// Default constructor
public jacobLinkedList() {
}
// appends the specified element to the end of this list.
public void add(Object data, int i) {
// Initialize Node only incase of 1st element
if (head == null) {
    head = new Node(data, i);
}
Node jacobTemp = new Node(data, i);
Node jacobCurrent = head;
if (jacobCurrent != null) {
while (jacobCurrent.getNext() != null) {
    jacobCurrent = jacobCurrent.getNext();
}
jacobCurrent.setNext(jacobTemp);
}
// increment the number of elements variable
incrementCounter();
}

private static int getCounter() {
    return counter;
}

private static void incrementCounter() {
    counter++;
}

private void decrementCounter() {
    counter--;
}

// inserts the specified element at the specified position in this list
public void insert(Object data, int i) {
    Node jacobTemp = new Node(data, i);
    Node jacobCurrent = head;

    if (jacobCurrent != null) {
        // crawl to the requested index or the last element in the list, whichever comes first
        for (int z = 0; z < i && jacobCurrent.getNext() != null; i++) {
            jacobCurrent = jacobCurrent.getNext();
        }
        }

    // set the new node's next-node reference to this node's next-node reference
    jacobTemp.setNext(jacobCurrent.getNext());

    // reference to new node
    jacobCurrent.setNext(jacobTemp);

    // increment the number of elements variable
    incrementCounter();
}

// removes the element at the specified position in this list.
public boolean remove(int index) {

    // if the index is out of range, exit
    if (index < 1 || index > size())
        return false;

    Node jacobCurrent = head;
    if (head != null) {
        for (int i = 0; i < index; i++) {
            if (jacobCurrent.getNext() == null)
                return false;
            jacobCurrent = jacobCurrent.getNext();
        }
        jacobCurrent.setNext(jacobCurrent.getNext().getNext());
        decrementCounter();
        return true;
    }
    return false;
}

// returns the number of elements in this list.
public int size() {
    return getCounter();
}

public String toString() {
    String output = "";

    if (head != null) {
        Node jacobCurrent = head.getNext();
        while (jacobCurrent != null) {
            output += "[" + jacobCurrent.getData().getClass() + "]";
            jacobCurrent = jacobCurrent.getNext();
        }

    }
    return output;
}

public class Node {
    // reference to the next node in the chain
    Node next;
    Object data;
    // Node constructor
    public Node(Object dataValue, Class<Integer> class1) {
        next = (Node) null;
        data = dataValue;
    }

    // Node contructor to point towards
    @SuppressWarnings("unused")
    public Node(Object dataValue, Node ranking) {
        next = ranking;
        data = dataValue;
    }
    public Node(Object data, int i) {
        // TODO Auto-generated constructor stub
    }
    public Object getData() {
        return data;
    }

    @SuppressWarnings("unused")
    public void setData(Object dataValue) {
        data = dataValue;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node nextValue) {
        next = nextValue;
    }

    }
    }

3 个答案:

答案 0 :(得分:0)

从它的外观来看,getData对象中的Node方法很可能返回null。那是因为你忘了在构造函数中用对象和整数设置数据。

public Node(Object data, int i) {
        this.data = data;
    }

我不知道整数是什么。

答案 1 :(得分:0)

在此行的toString()方法中:

output += "[" + jacobCurrent.getData().getClass() + "]";

您需要检查getData()何时返回null,这在大多数情况下会为您的输入提供。

解决方案是在将字符串添加到null之前检查output。不知道你想要它,添加字符串"null"或跳过这种情况或修复输入。

答案 2 :(得分:0)

使用:

/**
 * Created by MR on 4/27/2016.
*/
import java.util.Collections;
import java.util.List;
public class Test {
    public static jacobLinkedList jacobList;
    @SuppressWarnings("unchecked")
    public static <T> void main(String[] args) {
        // this is the default constructor.
        jacobList = new jacobLinkedList();
        // add elements to the list.
        jacobList.add("MegaDeth",1993);
        jacobList.add("Slayer",1992);
        jacobList.add("Scar Symmetry",2002);
        jacobList.add("Gojira",2004);
        jacobList.add("Amon Amarth",1997);
        System.out.println("Print: jacobList:" + jacobList);
        System.out.println(".size():" + jacobList.size());
        System.out.println(".remove(2):" + jacobList.remove(2) + " (element removed)");
        System.out.println("Print again:" + jacobList);
        System.out.println(".remove(1):" + jacobList.remove(1) + " (element removed)");
        System.out.println("Print again:" + jacobList);
        System.out.println(".remove(1):" + jacobList.remove(1) + " (element removed)");
        System.out.println("Print again:" + jacobList);
    }
}

class jacobLinkedList {
    private static int counter;
    private Node head;

    // Default constructor
    public jacobLinkedList() {

    }
    // appends the specified element to the end of this list.
    public void add(Object data, int i) {
    // Initialize Node only incase of 1st element
        if (head == null) {
            head = new Node(data, i);
        }
        Node jacobTemp = new Node(data, i);
        Node jacobCurrent = head;
        if (jacobCurrent != null) {
            while (jacobCurrent.getNext() != null) {
                jacobCurrent = jacobCurrent.getNext();
            }
            jacobCurrent.setNext(jacobTemp);
        }
      // increment the number of elements variable
        incrementCounter();
    }

    private static int getCounter() {
        return counter;
    }

    private static void incrementCounter() {
        counter++;
    }

    private void decrementCounter() {
        counter--;
    }

    // inserts the specified element at the specified position in this list
    public void insert(Object data, int i) {
        Node jacobTemp = new Node(data, i);
        Node jacobCurrent = head;

        if (jacobCurrent != null) {
            // crawl to the requested index or the last element in the list,           whichever comes first
            for (int z = 0; z < i && jacobCurrent.getNext() != null; i++) {
                jacobCurrent = jacobCurrent.getNext();
            }
        }

        // set the new node's next-node reference to this node's next-node reference
        jacobTemp.setNext(jacobCurrent.getNext());

        // reference to new node
        jacobCurrent.setNext(jacobTemp);

        // increment the number of elements variable
        incrementCounter();
    }

    // removes the element at the specified position in this list.
    public boolean remove(int index) {

        // if the index is out of range, exit
        if (index < 1 || index > size())
            return false;

        Node jacobCurrent = head;
        if (head != null) {
            for (int i = 0; i < index; i++) {
                if (jacobCurrent.getNext() == null)
                    return false;
                jacobCurrent = jacobCurrent.getNext();
            }
            jacobCurrent.setNext(jacobCurrent.getNext().getNext());
            decrementCounter();
            return true;
        }
        return false;
    }

    // returns the number of elements in this list.
    public int size() {
        return getCounter();
    }

    public String toString() {
        String output = "";

        if (head != null) {
            Node jacobCurrent = head.getNext();
            while (jacobCurrent.getData() != null) {
                output += "[" + jacobCurrent.getData().getClass() + "]";
                jacobCurrent = jacobCurrent.getNext();
            }

        }
        return output;
    }

    public class Node {
        // reference to the next node in the chain
        Node next;
        Object data;
        // Node constructor
        public Node(Object dataValue, Class<Integer> class1) {
            next = (Node) null;
            data = dataValue;
        }

        // Node contructor to point towards
        @SuppressWarnings("unused")
        public Node(Object dataValue, Node ranking) {
            next = ranking;
            data = dataValue;
        }
        public Node(Object data, int i) {
            // TODO Auto-generated constructor stub
        }
        public Object getData() {
            return data;
        }

        @SuppressWarnings("unused")
        public void setData(Object dataValue) {
            data = dataValue;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node nextValue) {
            next = nextValue;
        }

    }
}

我希望这有助于你