Java btree和nullpointerexception

时间:2012-10-09 08:32:50

标签: java nullpointerexception binary-search-tree

我在第

行收到NullPointerException
GUI getText (tr.search(tr, txtFindf.getText().charAt(0),txtFindf.getText())

和等于的行。我的程序是二叉树实现:

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;   
}
}

2 个答案:

答案 0 :(得分:1)

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

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

所以你有两种可能性。检查一下。

答案 1 :(得分:0)

找出哪个变量为null。

如果没有帮助就无法做到,请使用调试器并暂停未捕获的NullPointerExceptions。