Java抛出空指针异常并终止,即使我希望有空指针也是如此

时间:2018-08-07 08:59:10

标签: java nullpointerexception

这似乎很愚蠢,但是我目前正在尝试构建一个链表,其中列表中应该有一些元素指向空(例如,最后一个元素指向下一个为null,第一个元素指向为下一个)其先前元素)

接口:

public interface StringList {

    void appendString(String text);

    String getStringAt(int i);

    void insertStringAt(int i, String text);

    void insertStringListAt(int i, StringList list);

    String replaceStringAt(int i, String text);

    String removeStringAt(int i);

    String getFirstString();

    String getLastString();

    StringList reverseStringList();

    int getIndexOfString(String text, int startIndex);

    int countElements();

    String[] toStringArray();

    String toString();

}

LinkedStringList类:

public class LinkedStringList implements StringList {
    /**
     * An item in the linked list.
     * 
     * @author Marcus
     */
    public static class Item {

        /**
         * The string stored in the item.
         */
        private final String string;

        /**
         * The next element in the list or
         * null, if this is the last element.
         */
        private Item next;

        private Item predecessor;

        /**
         * Creates an item to store the string.
         * 
         * @param string The string to store.
         */
        public Item(String string) {
            this.string = string;
        }

        /**
         * Returns the string stored in the item.
         * 
         * @return The string stored in the item.
         */
        public String getString() {
            return string;
        }

        /**
         * Returns the next item in the list or
         * null, if this is the last item.
         * 
         * @return The next item.
         */
        public Item getNext() {
            return next;
        }

        public Item getPredecessor() {
            return predecessor;
        }
        /**
         * Sets the next item of the list.
         * 
         * @param next The value for the next item.
         */
        public void setNext(Item next) {
            this.next = next;
        }

        public void setPredecessor(Item predecessor) {
            this.predecessor = predecessor;
        }
    }

    /**
     * The head (i.e. first element) of the
     * list or null, if the list is empty.
     */
    private Item head;

    private Item currentItem;
    /**
     * Creates a new, empty list.
     */
    public LinkedStringList() { 
        super();
    }

    @Override
    public String getFirstString() {
        // check if empty
        if (head.getNext() == null) return null;
        // else return string in first element
        return head.getString();
    }

    @Override
    public void appendString(String text) { 

        Item appendedItem = new Item(text);
        // if the list is empty
        if(head.getNext() == null)  {
            appendedItem.setPredecessor(head);
            appendedItem.setNext(null);
            currentItem = appendedItem;
        }
        // else add new Item to the list
        else {
            currentItem.setNext(appendedItem);
            appendedItem.setPredecessor(currentItem);
            appendedItem.setNext(null);
            currentItem = appendedItem;
        }

    }

主类:

public class Main {

    public static void main(String[] args) {
        testLinkedStringList();
    }

    public static void testLinkedStringList() {
        LinkedStringList testList = new LinkedStringList();
        testList.appendString("Hello");
        testList.appendString("what's");
        testList.appendString("up?");
        System.out.println(testList.toString());
        System.out.println(testList.getFirstString());
    }
}

运行时会引发异常:

Exception in thread "main" java.lang.NullPointerException
    at LinkedStringList.appendString(LinkedStringList.java:98)
    at Main.testLinkedStringList(Main.java:10)
    at Main.main(Main.java:5)

因此if(head.getNext() == null)已经不起作用。为什么我不能进行此检查?当head初始化时,我看到它的方式是next为null。

0 个答案:

没有答案
相关问题