【问题标题】:Java btree and nullpointerexceptionJava btree 和空指针异常
【发布时间】:2012-10-09 08:32:49
【问题描述】:

我收到了一个NullPointerException

GUI getText (tr.search(tr, txtFindf.getText().charAt(0),txtFindf.getText())

并与equals一致。我的程序是二叉树实现:

public class BTree {

private char value;
private BTree left;
private BTree right;
private String indent;  
private boolean cheak;
private String searchres; 

public BTree pushInTree (BTree node,char leter,String word) {
    if(node == null){
    node = new BTree();
    node.value = leter;
    node.indent = word;
    return node;
    }
    else 
        if (node.value > leter) node.left = pushInTree(node.left, leter, word);
        else node.right = pushInTree(node.right, leter, word);
    return node;
}
public void output(BTree node) {
    if (node == null) return;
    System.out.println(node.indent+"");
    if (node.left != null ) output(node.left);
    if (node.right != null ) output(node.right);
}
    public void output2(BTree node) {   
    System.out.print(node.value+" ");
    if (node.left != null ) output2(node.left);
    if (node.right != null ) output2(node.right);

}
public boolean search (BTree node,char leter,String word){
    if (node == null)return cheak;
    if (node.indent.equals(word)){ \\error here !!!
        cheak=true;
        searchres = node.indent;
    }
    if (node.value > leter) search(node.left, leter,word);
    else search(node.right, leter,word);
    return cheak;
}
public String result() {
    return searchres;   
}
}

【问题讨论】:

    标签: java nullpointerexception binary-search-tree


    【解决方案1】:

    if (node.indent.equals(word)) 抛出空指针时,这意味着以下三种情况之一:

    1. node is null
    2. indent is null
    3. word is null.
    
    1. cannot be (because you have a null check).
    2. possible
    3. possible
    

    所以你有两种可能。检查它们。

    【讨论】:

      【解决方案2】:

      找出哪个变量为空。

      如果没有帮助就无法完成,请使用调试器并在未捕获的 NullPointerExceptions 上停止。

      【讨论】:

        猜你喜欢
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多