javatreebinary-search-tree

Bad Operand Types for Binary Operator ">"?


I am writing a BST Program. I get the error:

"Bad Operand Types for Binary Operator ">"

first type: java.lang.Object

second type: java.lang.Object"

This is the method where it gives me the error:

public void placeNodeInTree(TreeNode current, TreeNode t)                                                                    
{   
    if(current == null)
        current = t;
    else{
       if(current.getValue() > t.getValue()) 
            current.setRight(t);
       if(current.getValue() < t.getValue()) 
            current.setLeft(t);  
        }
}

getValue() has a return type of Object, thus the java.lang.Object types. This is the first time I have ever seen this error. Can anyone give me some background on this error? Thanks


Solution

  • Java doesn't support operator overloading, so the < operator is not defined for non-primitive types. You probably want to use the Comparable<T> interface instead.