[英]Condition in while loop doesn't work?
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;
}
}
the exception: 例外:
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)
It throws NullPointerException
on System.out.println(p1.getElement().toString());
它将在
System.out.println(p1.getElement().toString());
上引发NullPointerException
System.out.println(p1.getElement().toString());
. 。 It means that it doesn't pay attention to the condition of while loop?
这意味着它不注意while循环的条件吗?
p1
is not null
, but p1.getElement()
returned null
. p1
不为null
,但p1.getElement()
返回null
。
Remove the toString()
call. 删除
toString()
调用。 You don't need it there in sysout. 您在sysout中不需要它。 It will then just print
null
as "null". 然后它将仅将
null
打印为“ null”。
The condition in the while
loop does work. while
循环中的条件确实起作用。 The NullPointerException
is thrown on p1.getElement()
, which is null
and therefore cannot be dereferenced. NullPointerException
抛出在p1.getElement()
,它为null
,因此无法取消引用。 If p1
were null
, the line above it ( p2 = p1.next
) would fail. 如果
p1
为null
,则其上方的行( p2 = p1.next
)将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.