我有这个构造函数:
public class SentinelT<T> extends NodeT<T> {
//constructs an empty Sentinel linked to no other Nodes
public SentinelT() {
super(null, null, null);
this.prev = this;
this.next = this;
}
...
}
因此,每当我尝试更改 this.prev 或 this.next 的值,或者尝试对这些值使用布尔运算符时,都会得到 NullPointerException。例如:
public boolean isEmpty() {
return this.prev == this && this.next == this;
}
抛出 NullPointerException。我有一种感觉,我只是不了解超级构造函数或空值...感谢您的帮助。
*编辑:添加了 NodeT 构造函数,将添加引发异常的实例化
//NodeT class for a doubly linked list of T
public class NodeT<T> {
T data;
NodeT<T> prev;
NodeT<T> next;
//constructs a Node object
public NodeT(T data, NodeT<T> prev, NodeT<T> next) {
this.data = data;
this.prev = prev;
this.next = next;
}
*edit2:不同的类,假设 stringHeader 是发生在 SentinelT 中的类的一个字段 stringHeader = new SentinelT();
public void testIsEmpty(Tester t) {
initData();
t.checkExpect(stringHeader.isEmpty(), true);
}