Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;NullPointerException“;对双链接列表进行排序时_Java_List_Nullpointerexception - Fatal编程技术网

Java &引用;NullPointerException“;对双链接列表进行排序时

Java &引用;NullPointerException“;对双链接列表进行排序时,java,list,nullpointerexception,Java,List,Nullpointerexception,我在学习指针的概念。下面的列表有一些假设的指针,一个指向上一个数字,另一个指向下一个数字。还有一个元素指向当前的数字。当我运行代码时,控制台返回“NullPointerException”。代码背后的逻辑对我来说似乎没问题。我是一个初学者,非常感谢您的帮助。我试着尽可能多地注释代码,以便更清楚地理解。多谢各位 aux = start; //aux points to the start of the list. both of them are of the type 'List'

我在学习指针的概念。下面的列表有一些假设的指针,一个指向上一个数字,另一个指向下一个数字。还有一个元素指向当前的数字。当我运行代码时,控制台返回“NullPointerException”。代码背后的逻辑对我来说似乎没问题。我是一个初学者,非常感谢您的帮助。我试着尽可能多地注释代码,以便更清楚地理解。多谢各位

aux = start; //aux points to the start of the list. both of them are of the type 'List'
            int prev = start.num; //prev points to the number on the start position. both of them are of the type 'int'
            while (aux.next != null) { //aux.next means the pointer to the next element in the list
                if (aux.num >= aux.next.num) { //if the current number is greater than the next number, or equal
                    prev = aux.next.num; //prev recieves the lower number
                    aux.next.num = aux.num; //the greater number is put after the lower number
                    aux.num = prev; //the lower number is put before the greater number
                }
                aux = aux.next; //the current element takes a step to the next element so I can progress through the list
                aux.num = aux.next.num;
        }

编辑:我忘了说这个列表必须按升序排列。它还必须按另一种方法降序排列。

您正在检查
aux.next
是否为空,但您没有检查
aux.prox
的此条件(我相信,
prox
是一个指针)

编辑:

所以。。。看

aux = aux.next;
aux.num = aux.next.num; 

您必须检查new
aux.next
是否不为NULL

您正在检查
aux.next
是否不为NULL,但您没有检查
aux.prox
的此条件(我相信,
prox
是一个指针)

编辑:

所以。。。看

aux = aux.next;
aux.num = aux.next.num; 

您必须检查new
aux.next
是否不为NULL

您正在检查
aux.next
是否不为NULL,但您没有检查
aux.prox
的此条件(我相信,
prox
是一个指针)

编辑:

所以。。。看

aux = aux.next;
aux.num = aux.next.num; 

您必须检查new
aux.next
是否不为NULL

您正在检查
aux.next
是否不为NULL,但您没有检查
aux.prox
的此条件(我相信,
prox
是一个指针)

编辑:

所以。。。看

aux = aux.next;
aux.num = aux.next.num; 

您必须检查new
aux.next
是否为NULL

它发生在while循环的最后两行

aux = aux.next; //the current element takes a step to the next element so I can progress through the list
aux.num = aux.next.num;
在这里指定
aux=aux.next
,然后执行
aux.num=aux.next.num。
如果
aux
现在是列表中的最后一个元素,
aux.next
null
aux.next.num
将抛出NPE

为了防止出现这种情况,您可以在访问
num
之前进行另一项检查:

aux = aux.next;
if(aux.next != null) {
    aux.num = aux.next.num;
}
else {
    break;
}

它发生在while循环的最后两行

aux = aux.next; //the current element takes a step to the next element so I can progress through the list
aux.num = aux.next.num;
在这里指定
aux=aux.next
,然后执行
aux.num=aux.next.num。
如果
aux
现在是列表中的最后一个元素,
aux.next
null
aux.next.num
将抛出NPE

为了防止出现这种情况,您可以在访问
num
之前进行另一项检查:

aux = aux.next;
if(aux.next != null) {
    aux.num = aux.next.num;
}
else {
    break;
}

它发生在while循环的最后两行

aux = aux.next; //the current element takes a step to the next element so I can progress through the list
aux.num = aux.next.num;
在这里指定
aux=aux.next
,然后执行
aux.num=aux.next.num。
如果
aux
现在是列表中的最后一个元素,
aux.next
null
aux.next.num
将抛出NPE

为了防止出现这种情况,您可以在访问
num
之前进行另一项检查:

aux = aux.next;
if(aux.next != null) {
    aux.num = aux.next.num;
}
else {
    break;
}

它发生在while循环的最后两行

aux = aux.next; //the current element takes a step to the next element so I can progress through the list
aux.num = aux.next.num;
在这里指定
aux=aux.next
,然后执行
aux.num=aux.next.num。
如果
aux
现在是列表中的最后一个元素,
aux.next
null
aux.next.num
将抛出NPE

为了防止出现这种情况,您可以在访问
num
之前进行另一项检查:

aux = aux.next;
if(aux.next != null) {
    aux.num = aux.next.num;
}
else {
    break;
}

在哪一行抛出NullPointerException?您的代码不正确。。。请阅读一个排序算法。。。从简单的冒泡排序开始,然后尝试用数组实现它。。。然后试着用链表来做。。。我们可以给你代码,但这无助于你学习…@Petter在“aux.num=aux.next.num;”,我的问题代码块的第10行。在使用任何参考之前,最好检查一下!=null&确切地告诉npe在哪一行?在该行之前,您可以检查(aux!=null&&aux.next!=null)是否在哪一行抛出NullPointerException?您的代码不正确。。。请阅读一个排序算法。。。从简单的冒泡排序开始,然后尝试用数组实现它。。。然后试着用链表来做。。。我们可以给你代码,但这无助于你学习…@Petter在“aux.num=aux.next.num;”,我的问题代码块的第10行。在使用任何参考之前,最好检查一下!=null&确切地告诉npe在哪一行?在该行之前,您可以检查(aux!=null&&aux.next!=null)是否在哪一行抛出NullPointerException?您的代码不正确。。。请阅读一个排序算法。。。从简单的冒泡排序开始,然后尝试用数组实现它。。。然后试着用链表来做。。。我们可以给你代码,但这无助于你学习…@Petter在“aux.num=aux.next.num;”,我的问题代码块的第10行。在使用任何参考之前,最好检查一下!=null&确切地告诉npe在哪一行?在该行之前,您可以检查(aux!=null&&aux.next!=null)是否在哪一行抛出NullPointerException?您的代码不正确。。。请阅读一个排序算法。。。从简单的冒泡排序开始,然后尝试用数组实现它。。。然后试着用链表来做。。。我们可以给你代码,但这无助于你学习…@Petter在“aux.num=aux.next.num;”,我的问题代码块的第10行。在使用任何参考之前,最好检查一下!=null&确切地告诉npe在哪一行?在那一行之前,您可以检查(aux!=null&&aux.next!=null)对不起,“prox”不存在,键入它是我的错误。你说的“prox”是“下一个”。我在代码上修正了它。是的,我检查了它,它进入了一个无限循环。很抱歉,“prox”不存在,是我的错误。你说的“prox”是“下一个”。我在代码上修正了它。是的,我检查了它,它进入了一个无限循环。很抱歉,“prox”不存在,是我的错误。这个