0

this might seem stupid, but I am currently trying to build a linked list, where there should be elements in the list that point to null (for example the last element points to null as the next, the first points to null as its prior element)

Interface:

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();

}

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

    }

Main Class:

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

When I run it it throws the exception:

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)

So if(head.getNext() == null) is already not working. Why can I not do this check? The way I see it when head is initialized it has the attribute next as null.

Crenshaw
  • 13
  • 5
  • 5
    `head` is null, thus `head.getNext()` throws NPE. – FThompson Aug 07 '18 at 09:01
  • replace `@Override public void appendString(String text) { Item appendedItem = new Item(text); if(head == null) {appendedItem.setNext(null); appendedItem.setPredecessor(null); head = appendedItem; }// if the list is empty else 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; }}` – random_user Aug 07 '18 at 09:18
  • Thanks for answers, I understand now. I am wondering tho, what is the difference between doing `Item head = new Item("")` and `Item head;` does `head` in the latter case have the attributes defined in the Item class, but set to null, or does it not have any attributes at all? – Crenshaw Aug 08 '18 at 11:31

0 Answers0