有空指针异常

时间:2017-02-20 13:39:41

标签: java nullpointerexception

尝试创建一个对分数进行排序的BST。 treenode类

public class TreeNode<E> {
protected E element;
protected TreeNode<E> left;
protected TreeNode<E> right;
public TreeNode(E e){
    element = e;
}
}

分数类......

    import java.util.ArrayList;
import java.util.Stack;

public class Fractions {
private String fractionS;
private ArrayList<String> tokenArray;
public Fractions(String s){
    this.fractionS = s;
}

public String toString(){

    return fractionS;

}

public String extractNumber(int n, String s){
    String num = new String ();
    char c;
    for (int i = n; i<s.length(); i++){
        c = s.charAt(i);
        if (c >= '0' && c <= '9'){
            num+=String.valueOf(c);
        }else{
            break;
        }
    }
    return num;
}

public Double getNumbers(){
    char c;
    Stack<String> numStack = new Stack<String>();
    for (int i = 0; i<this.fractionS.length(); i++){
        c = this.fractionS.charAt(i);
        if (c >= '0' && c<= '9'){
            numStack.push(extractNumber(i, this.fractionS));
            i += numStack.peek().length()-1;
        }
    }
    Double denominator = Double.parseDouble(numStack.pop());
    Double numerator = Double.parseDouble(numStack.pop());
    Double solution = numerator/denominator;
    return solution;
}


public int compareTo(Fractions f) {
    Double d1 = this.getNumbers();
    Double d2 = f.getNumbers();
    if (d1<=d2){
        return 1;
    }else{
    return 0;
    }
}

我在调用NullPointerException方法时获得了compareTo。试着找出NullPointerException。我自己可以做其余的事。这是一堂课,我不想遇到麻烦。

包括分数的实例化...... 我还对分数类进行了更改。

public void createTree(){
tokenizer();
Stack<String> numbers = new Stack<String>();
int count = 0;
for (int i = 0; i<tokenArray.size(); i++){
    char c = tokenArray.get(i).charAt(0);
    if (c == '/'){
        count+= 1;
    }else if (c >= '0' && c <= '9'){
        numbers.push(tokenArray.get(i));
    }
}
for (int i = 0; i <count; i++){
    String denominator = numbers.pop();
    String numerator = numbers.pop();
    insert(new Fractions(numerator + "/" + denominator));
}

2 个答案:

答案 0 :(得分:1)

做类似的事情:

    boolean left = false;
    parent = current = root;
    while (current!=null){
        if (left = (f.compareTo(current.element) == 1)){
            parent = current;
            current = current.left;
        }else{
            parent = current;
            current = current.right;
        }
    }
    if (left){
        parent.left = new TreeNode<Fractions>(f);
    }else{
        parent.right = new TreeNode<Fractions>(f);
    }

甚至:

    boolean left = false;
    current = root;
    do {
        left = f.compareTo(current.element) == 1;
        parent = current;
        if (left){
            current = current.left;
        }else{
            current = current.right;
        }
    } while (current!=null);
    if (left){
        parent.left = new TreeNode<Fractions>(f);
    }else{
        parent.right = new TreeNode<Fractions>(f);
    }

答案 1 :(得分:0)

您的compareTo方法根本没有检查参数Fractions f是否为空,因此如果f == null,它会尝试调用f.evaluate() ,它会抛出NullPointerException(NPE),因为它没有引用Fractions对象来调用evaluate()

而是更新您的if条件以首先检查:

if (f != null && this.evaluate() <= f.evaluate()) {
    // ...
}

您的insert方法也是如此。你应该能够插入一个空的分数吗?我不这么认为。