1

I am trying to build a simple LinkedList in Java which takes chunks of a String. I am getting NullPointerException when i try to pop the elements. Below is my code. Please help me out.

public class LinkedList {

    private class Node {

        private String info;
        private Node next;

        Node() {
            this.info = null;
            this.next = null;
        }

        Node(String item, Node next) {
            this.info = item;
            this.next = next;
        }

    }

    // private Node start;

    Node start = new Node();
    Node end = new Node();
    Node top;

    public void addNode(String item) {
        if (start.next == null) {
            top = new Node(item, end);
            start.next = top;
            return;
        }

        if (start.next != null) {

            top.next = new Node(item, end);
            top = top.next;
            // top.next = end;

        }
    }

    public String[] pop() {
        String[] result = null;
        Node popping = start;
        while (popping.next != end) {
            popping = popping.next;
            int count = 0;
            System.out.println(count);
            result[count] = popping.info;

            count++;
        }

        return result;
    }

    public static void main(String args[]) {
        LinkedList kds = new LinkedList();
        for (String s : "kds on a roll".split(" ")) {
            kds.addNode(s);
            // System.out.println(kds.toString());
        }

        String[] s = null;
        System.out.println("Popping Begins");
        s = kds.pop();
        System.out.println(s);
    }
}
Kick Buttowski
  • 6,371
  • 12
  • 34
  • 54

0 Answers0