我正在尝试在LinkedList
中构建一个简单的Java
,其中包含一个字符串块。
当我尝试弹出元素时,我得到NullPointerException
。以下是我的代码。请帮帮我。
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);
}
}