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循環的條件?
一般來說,當你將一個對象傳遞給PrintWriter的函數println()(比如`System.out`)時,你不需要對對象執行`toString()`調用。該函數會自動將對象隱式轉換爲一個「String」。 – 2010-11-27 05:18:54
By [`String#valueOf()`](http://download.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29),實際上,它進行隱式空指針檢查。 – BalusC 2010-11-27 05:20:12