private void Scan(DoublyLinkedList dList) { // T(n) = O(n)
DNode p1 = dList.getFirst();
while (p1 != null) {
DNode p2 = p1.next;
System.out.println(p1.getElement().toString()); // <--- Here it throws NullPointerException.
if (p2.next != null) {
DNode p3 = p2.next;
if (p3.getElement() != null) {
boolean b = Determinate.isPointRightSide(p1.getElement(), p2.getElement(),p3.getElement());
if (b == true) {
p1 = p1.next;
} else {
p1.next = p3;
p3.prev = p1;
dList.remove(p2);
p1 = p1.prev;
}
} else break;
}else break;
}
}
例外:
run:
Exception in thread "main" java.lang.NullPointerException
X :8.0 Y: 9.0angle0.0lol
at ConvexHull.GrahamVersion.Scan(GrahamVersion.java:102)
at ConvexHull.GrahamVersion.grahamScan(GrahamVersion.java:83)
at ConvexHull.GrahamVersion.<init>(GrahamVersion.java:25)
它在
NullPointerException
上抛出System.out.println(p1.getElement().toString());
。这意味着它不注意while循环的条件吗? 最佳答案
p1
不是null
,但是p1.getElement()
返回了null
。
删除toString()
呼叫。您在sysout中不需要它。然后它将仅将null
打印为“ null”。
关于java - while循环中的条件不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4290204/