-2

Trying to create a BST that sorts Fractions. The treenode class

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

The fractions class...

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

I get a NullPointerException on the call for compareTo method. Just trying to figure out the NullPointerException. I can do the rest myself. This is for a class, and I don't want to get in trouble.

Including the instantiating of fractions... I also made changes to fractions class.

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));
}
WinDows
  • 1
  • 1
  • 1
    A) use the java tag to indicate that you have a java problem ... but actually B) do prior research, like ... just searching for that exception name ... – GhostCat Feb 20 '17 at 13:41

2 Answers2

1

Do something like that:

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

Or even:

    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);
    }
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

Your compareTo method simply hasn't checked that the parameter Fractions f isn't null, thus if f == null, when it attempts to call f.evaluate(), it will throw a NullPointerException (NPE) since it doesn't have a reference to a Fractions object to call evaluate().

Instead update your if condition to check for this first:

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

The same goes with your insert method. Should you be able to insert a fraction that is null? I don't think so.

xlm
  • 6,854
  • 14
  • 53
  • 55
  • Adding checks for a null fraction would be good, but the fraction should not be null... I can't figure out why it's null. The insert method is being called by a for loop that goes through an arraylist of fractions. All those fractions should all be instantiated properly. – WinDows Feb 20 '17 at 14:11
  • Use a debugger (from your IDE i.e. Eclipse or IntelliJ) and set a break point on the call. – xlm Feb 20 '17 at 21:29
  • @WinDows you edit your Q and post additional code on instantiation of insertion. – xlm Feb 20 '17 at 23:13
  • I have added the instantiating point. And I made changes to Fractions. Fractions class is instantiated with a string representation of fractions, e.g "25/5" and its compareTo method compares two doubles generated from the string. I'll go ahead and also include the treenode class. Thanks for any help. – WinDows Feb 21 '17 at 13:53