【问题标题】:NullPointerException in BSTBST 中的 NullPointerException
【发布时间】:2016-05-03 12:38:40
【问题描述】:

我正在尝试编写一个函数来在二叉搜索树中查找键,但由于某种原因,每次键不存在时,它都会返回 NullPointerException 而不是返回 null。该功能似乎微不足道 - 有什么想法吗?

public class findFirstKey {

    static Node1 binSearch(Node1 root, int value){

        Node1 tempNode = root;

        while(tempNode!=null){  //  check while condition

            if(tempNode.getData()==value){
                return tempNode;
            }

            else if(value<tempNode.getData()){
                tempNode=tempNode.getLeft();
            }

            else if(value>tempNode.getData()){
                tempNode = tempNode.getRight();
            }

        }
        return null;
    }




    public static void main(String[] args) {

        //  Tree 1. page 258

        Node1 root = new Node1(19);
        Node1 seven = new Node1(7);
        Node1 three = new Node1(3);
        Node1 two = new Node1(2);
        Node1 five = new Node1(5);
        Node1 eleven = new Node1(11);
        Node1 seventeen = new Node1(17);

        Node1 thirteen = new Node1(13);     //  CHANGE THIS TO 9, IT STILL COMES OUT TRUE
        Node1 fortythree = new Node1(43);
        Node1 twentythree = new Node1(23);
        Node1 thirtyseven = new Node1(37);
        Node1 twentynine = new Node1(29);

        Node1 thirtyone = new Node1(31);
        Node1 fortyone = new Node1(41);
        Node1 fortyseven = new Node1(47);
        Node1 fiftythree = new Node1(53);


        root.setLeft(seven);
        seven.setLeft(three);
        three.setLeft(two);
        three.setRight(five);
        seven.setRight(eleven);
        eleven.setRight(seventeen);
        seventeen.setLeft(thirteen);
        root.setRight(fortythree);
        fortythree.setLeft(twentythree);
        twentythree.setRight(thirtyseven);
        thirtyseven.setLeft(twentynine);
        twentynine.setRight(thirtyone);
        thirtyseven.setRight(fortyone);
        fortythree.setRight(fortyseven);
        fortyseven.setRight(fiftythree);

        System.out.println(binSearch(root, 14).getData());

    }

    static class Node1{

        private int data;
        private Node1 left;
        private Node1 right;


        Node1(int data){

            this.data=data;
            this.left=null;
            this.right=null;
        }


        void setLeft(Node1 left){
            this.left = left;
        }

        void setRight(Node1 right){
            this.right = right;
        }

        int getData(){  
            return this.data;
        }

        Node1 getLeft(){
            return this.left;
        }

        Node1 getRight(){
            return this.right;
        }

    }
}

【问题讨论】:

  • binSearch(root, 14).getData() 将抛出 NullPointerException 如果 binSearch(root, 14) 返回 null。

标签: java nullpointerexception binary-search-tree


【解决方案1】:

您不能在空对象上调用 getData()。 添加这样的检查:

Node1 target = binSearch(root, 14);
if(target != null)
    System.out.println(target.getData());
else{
    //do or print something else...
}

【讨论】:

  • 啊,好吧。我修好了它。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 2013-02-21
  • 2021-10-17
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多