【问题标题】:Exception in thread "main" java.lang.NullPointerException when casting integers at an if statement在 if 语句中转换整数时,线程“main”java.lang.NullPointerException 中的异常
【发布时间】:2013-01-30 20:31:21
【问题描述】:

问题出在partition(),它将读取LinkedList 节点并将该节点的数据值与输入的整数值进行比较。程序在比较语句node.data < x 处抛出NullPointerException。我无法弄清楚,有人可以帮助我吗?非常感谢。

package Chapter2;
import java.util.*;
public class LinkedList2<E>{
    static class LinkedListNode<E>{
        E data;
        LinkedListNode<E> next;
    }

    private LinkedListNode<E> head;
    private LinkedListNode<E> tail;

    public LinkedList2(){
        this.head = new LinkedListNode<E>();
        this.tail = new LinkedListNode<E>();
        head.next = tail;
    }
    public void addLast(E e){
        LinkedListNode<E> node = new LinkedListNode<E>();
        tail.data = e;
        tail.next = node;
        tail = node;
    }

    public void print(){
        LinkedListNode<E> curr = head.next;
        while(curr.next != null){
            System.out.print(curr.data + " ");
            curr = curr.next;
        }
        System.out.println("");
    }

    public LinkedListNode<Integer> partition(LinkedListNode<Integer> node, Integer x){
        LinkedListNode<Integer> beforeStart = null;
        LinkedListNode<Integer> beforeEnd = null;
        LinkedListNode<Integer> afterStart = null;
        LinkedListNode<Integer> afterEnd = null;

        while(node!= null){
            LinkedListNode<Integer> next = node.next;
            node.next = null;
            System.out.println(node.data);
            if(node.data < x){
                if(beforeStart == null){
                    beforeStart = node;
                    beforeEnd = beforeStart;
                }else{
                    beforeEnd.next = node;
                    beforeEnd = node;
                }
            }else{
                if(afterStart == null){
                    afterStart = node;
                    afterEnd = afterStart;
                }else{
                    afterEnd.next = node;
                    afterEnd = node;
                }
            }
            node = next;
        }
        if(beforeStart == null){
            return afterStart;
        }
        beforeEnd.next = afterStart;
        return beforeStart;
    }

    public static void main(String[] args){

        LinkedList2<Integer> listInt = new LinkedList2<Integer>();
        listInt.addLast(5);
        listInt.addLast(8);
        listInt.addLast(1);
        listInt.addLast(3);
        listInt.addLast(6);

        listInt.print();

        System.out.println(listInt.head.next.data);
        listInt.partition(listInt.head.next, 4);

        listInt.print();
    }
}

【问题讨论】:

  • System.out.println(node.data); 行在抛出错误之前会输出什么?完整的错误信息是什么?
  • 你应该解释分区应该做什么,否则你会得到答案“你得到了例外,因为该行中的节点为空”

标签: java if-statement nullpointerexception compare


【解决方案1】:

很简单,您正在检查 node.data 的值是否小于 x。由于 node.data 是 Integer 类型,它很可能为 null,这意味着该语句将抛出 NullPointer。您可以通过检查您的 while 语句来解决此问题:

while(node != null && node.data != null){
   /* Your Code */
}

【讨论】:

  • 哦,我明白了,在 while 循环的最后一次迭代中,节点将为空。但为什么会这样呢?迭代条件为node!=null。
  • 节点不会为空。由于 addLast 函数中的逻辑炸弹导致尾为空节点,Node.data 将为空。
  • 所以 while(node!= null&& node.data!=null) 很好用,谢谢。
【解决方案2】:

最终循环将node 设置为tailtail.datanull,因为它没有被设置——在addLast() 中,您将tail 设置为LinkedListNode,而不设置它的data。因此node != nullnode.datanull

【讨论】:

    【解决方案3】:

    我相信你设置的语句是导致这个空指针的原因

    你在这里设置下一个节点为空LinkedListNode&lt;Integer&gt; next = node.next;

    node.next = null;
    

    这将适用于第一次传递,但由于上述语句,第二次节点将为空 (node.next = null)

    调试它,你会看到。

    【讨论】:

    • node.next 为空,但 next 不为空,node 设置为 next,而不是 node.next
    【解决方案4】:

    您正在检查 node 是否不是 null,但没有检查 node.data 是否不是 null 也可以,所以在比较 null 时会出现 NullPointerException

    while 条件是正确的,因为您想遍历每个节点,但 if 条件应该是这样的:

    if(node.data != null && node.data < x){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 2023-03-10
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多