Java 使用compareTo的while循环的NullPointerException
对于我们的家庭作业,我必须把椅子上的物品添加到我们制作的双链接列表中;它必须按字母顺序排序,如果样式按字母顺序相同,我们就按颜色排序 当我试图通过循环时,我总是得到一个NullPointerException 这是我制作的双链接列表类: 类CDoublyLinkedList{ 首先是节点,最后是节点Java 使用compareTo的while循环的NullPointerException,java,nullpointerexception,compareto,doubly-linked-list,Java,Nullpointerexception,Compareto,Doubly Linked List,对于我们的家庭作业,我必须把椅子上的物品添加到我们制作的双链接列表中;它必须按字母顺序排序,如果样式按字母顺序相同,我们就按颜色排序 当我试图通过循环时,我总是得到一个NullPointerException 这是我制作的双链接列表类: 类CDoublyLinkedList{ 首先是节点,最后是节点 public CDoublyLinkedList(){ first = new Node(); last = new Node(); first.prev = last.next =
public CDoublyLinkedList(){
first = new Node(); last = new Node();
first.prev = last.next = null;
first.object = last.object = null;
first.next = last;
last.prev = first;
}
public boolean isEmpty(){
return first.object == null;
}
public void addFirst(Chair element){
Node insert = new Node();
insert.object = element;
insert.prev = null;
insert.next = first;
first.prev = insert;
first = insert;
}
public void add(Chair element){
if(isEmpty() || first.object.style.compareTo(element.style) > 0 || (first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) >= 0){
addFirst(element);
}else if(first.object.style.compareTo(element.style) <= 0){
Node temp = first;
Node insert = new Node(); insert.object = element;
while(first.object.style.compareTo(element.style) <= 0)
if(temp.hasNext())
temp = temp.next;
while(first.object.style.compareTo(element.style) == 0 && first.object.color.compareTo(element.color) <= 0)
if(temp.hasNext())
temp = temp.next;
insert.prev = temp.prev;
insert.next = temp;
temp.prev.next = insert;
temp.prev = insert;
}
}
public Chair removeFirst(){
Chair tobedeleted = first.object;
Node temp = first.next;
first = temp;
first.prev = null;
return tobedeleted;
}
private class Node{
Node next, prev;
Chair object;
public boolean hasNext(){
return next != null;
}
}
有人能给我解释一下为什么我总是犯这个错误吗?谢谢
编辑:
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
chairs.add(new Chair(temp[1], temp[2]));
java.lang.NullPointerException
at CDoublyLinkedList.add(Furnish2SS.java:119)
at Furnish2SS.main(Furnish2SS.java:23)
java.lang.NullPointerException
at CDoublyLinkedList.add(Furnish2SS.java:119)
at Furnish2SS.main(Furnish2SS.java:23)
我得到错误的原因是我没有在每次迭代中检查null。您说这是导致异常的代码行:
while(temp.object.style.compareTo(element.style) <= 0)
您可能应该在该行上设置一个调试器断点,并使用调试器来确定哪些值为空。但我很难在这里解释有关如何设置和使用调试器的完整说明,这并不意味着您不应该学习!您应该学习。有很多教程。请用谷歌搜索它。因此,与其编写关于调试的教程,不如在这里解释我将发布代码,告诉您哪个变量为空:
if (temp == null) {
System.out.println("temp is null");
} else if (temp.object == null) {
System.out.println("temp.object is null");
} else if (temp.object.style == null) {
System.out.println("temp.object.style is null");
}
if (element == null) {
System.out.println("element is null");
} else if (element.style == null) {
System.out.println("element.style is null");
}
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
{
if(temp.hasNext())
temp = temp.next;
if (temp == null) {
System.out.println("loop: temp is null");
} else if (temp.object == null) {
System.out.println("loop: temp.object is null");
} else if (temp.object.style == null) {
System.out.println("loop: temp.object.style is null");
}
if (element == null) {
System.out.println("loop: element is null");
} else if (element.style == null) {
System.out.println("loop: element.style is null");
}
}
如果使用上述代码语句替换这三行代码:
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
if(temp.hasNext())
temp = temp.next;
根据打印的语句,您将知道哪个变量是null。希望您可以从那里获取它。修复NullPointerException的通常方法是采取必要的步骤,确保在程序到达NullPointerException行时,有问题的null变量实际具有有效的非null值。y这是导致异常的代码行:
while(temp.object.style.compareTo(element.style) <= 0)
您可能应该在该行上设置一个调试器断点,并使用调试器来确定哪些值为空。但我很难在这里解释有关如何设置和使用调试器的完整说明,这并不意味着您不应该学习!您应该学习。有很多教程。请用谷歌搜索它。因此,与其编写关于调试的教程,不如在这里解释我将发布代码,告诉您哪个变量为空:
if (temp == null) {
System.out.println("temp is null");
} else if (temp.object == null) {
System.out.println("temp.object is null");
} else if (temp.object.style == null) {
System.out.println("temp.object.style is null");
}
if (element == null) {
System.out.println("element is null");
} else if (element.style == null) {
System.out.println("element.style is null");
}
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
{
if(temp.hasNext())
temp = temp.next;
if (temp == null) {
System.out.println("loop: temp is null");
} else if (temp.object == null) {
System.out.println("loop: temp.object is null");
} else if (temp.object.style == null) {
System.out.println("loop: temp.object.style is null");
}
if (element == null) {
System.out.println("loop: element is null");
} else if (element.style == null) {
System.out.println("loop: element.style is null");
}
}
如果使用上述代码语句替换这三行代码:
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
if(temp.hasNext())
temp = temp.next;
根据打印的语句,您将知道哪个变量是null。希望您可以从那里获取它。修复NullPointerException的通常方法是采取必要的步骤,以确保在程序到达NullPointerException行时,有问题的null变量实际具有有效的非null值。看看addFirstChair元素。该方法真的是一团糟。它创建了一个包含正确椅子的新节点。然后将其prev设置为null。然后将其设置在first旁边。这就是导致所有问题的原因。因为first指向一个空节点。您最终会得到以下结果:
第一个指向你的新节点。那个节点指向一个没有椅子的节点。那个节点再次指向最后一个
e:
你的整个代码看起来至少有两种不同的方法来实现你的列表,并将它们扔到一起。还有一些错误,但由于这是家庭作业,我想如果你先尝试修复它,也没那么糟糕
如果你不知道如何纠正,请在这里询问
PS:如果你注意到的话,很抱歉对我的回答进行了编辑和取消删除。我有点累了,一直在通过修复旧的错误来造成新的错误,直到我最终找出了所有这些的真正原因。看看addFirstChair元素。这个方法真的很糟糕。它创建了一个包含正确椅子的新节点。然后将其prev设置为null。然后将其设置为first旁边。这就是导致您所有问题的原因。因为first指向一个空节点。您将得到以下结果:
第一个指向你的新节点。那个节点指向一个没有椅子的节点。那个节点再次指向最后一个
e:
你的整个代码看起来至少有两种不同的方法来实现你的列表,并将它们扔到一起。还有一些错误,但由于这是家庭作业,我想如果你先尝试修复它,也没那么糟糕
如果你不知道如何纠正,请在这里询问
PS:如果你注意到的话,很抱歉对我的回答进行了编辑和取消删除。我有点累了,一直在通过修复旧的错误来造成新的错误,直到我最终找出了这一切的真正原因。你的一个或多个内部对象似乎先为空,或者对象或样式尝试将循环放入一个try catch中以获取内部异常您之所以出现错误,是因为某些内容为null。若要了解原因,请查看stacktrace并查看它显示异常发生在哪一行。项目中引用的一个对象尚未实例化或设置为null。请调试程序,并告诉我们在哪一行中发现了错误。这甚至可能会对您有所帮助解决你自己的问题。你能发布你的堆栈跟踪吗?关于你的信息,像first.object.style.这样的东西不是最好的代码实践。它是有效的,但是使用私有字段和g
etter和setter让您能够更好地控制谁可以访问某些变量,以及某人可以在其中写入什么。您的一个或多个内部对象似乎首先为null,或者对象或样式尝试将循环放入try catch以获取内部异常。您之所以会出错,是因为某些对象为null。要了解原因,请查看stacktrace并查看它表示异常发生在哪一行。项目中引用的某个对象尚未实例化或设置为null。调试程序,请告诉我们在哪一行中发现了错误。这甚至可以帮助您解决自己的问题。您可以发布堆栈跟踪吗?请参考first.object.style之类的内容。不是最佳的代码实践。它是有效的,但是使用带有getter和setter的私有字段可以让您更好地控制谁可以访问某些变量以及有人可以在这些变量中编写什么。事实是,我只是将这些都放在了一个try-catch中,即使它抛出了一个异常,while循环中的代码也可以工作。我还尝试检查temp==null,但没有打印任何内容。我修复了它。出于某种原因,代码运行正常,但引发了NullPointerException。我刚取出堆栈跟踪,它按我的要求打印出信息。@prunes4u这不是固定的,这是一个丑陋的黑客!为自己的工作感到自豪,不要使用try{}catch{}来隐藏程序中不应该出现的错误如果我是你的老师,我会因为你提交的代码在正常操作过程中出错而给你打分。坚强的爱!:我尝试检查temp==null、element==null和temp.object==null。他们都没有打印出消息,所以我不知道是什么抛出空值。另外,由于它抛出和异常,循环中的代码不应该运行,但它运行了。那么,剩下的就是temp.object.style。唯一可以为null的是temp.object和temp.object.style。问题是,我只是把这一切都放在一个try-catch中,即使它抛出了一个异常,while循环中的代码也可以工作。我还尝试检查temp==null,但没有打印任何内容。我修复了它。出于某种原因,代码运行正常,但引发了NullPointerException。我刚取出堆栈跟踪,它按我的要求打印出信息。@prunes4u这不是固定的,这是一个丑陋的黑客!为自己的工作感到自豪,不要使用try{}catch{}来隐藏程序中不应该出现的错误如果我是你的老师,我会因为你提交的代码在正常操作过程中出错而给你打分。坚强的爱!:我尝试检查temp==null、element==null和temp.object==null。他们都没有打印出消息,所以我不知道是什么抛出空值。另外,由于它抛出和异常,循环中的代码不应该运行,但它运行了。那么,剩下的就是temp.object.style。唯一可以为null的是temp.object和temp.object.style。实际上+1可以帮助他理解代码的逻辑。我不够勇敢+1感谢他在代码逻辑方面的实际帮助。我不够勇敢!
while(temp.object.style.compareTo(element.style) <= 0) //This is where the nullPointerException occurs
if(temp.hasNext())
temp = temp.next;