0

TreeComparable is a Comparable interface.

The Error:

java.lang.String cannot be cast to TreeComparable

This is the line giving me the error

if (((TreeComparable) r.getInfo()).compareTo((TreeComparable) p.getInfo()) < 0 )

And here is the method for that line:

public void insertBST(Object o) {
    ObjectTreeNode p, q;

    ObjectTreeNode r = new ObjectTreeNode(o);
    if (root == null)
        root = r;
    else {
        p = root;
        q = root;
        while (q != null) {
            p = q;
            if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0 )
                q = p.getLeft();
            else
                q = p.getRight();
        }
        if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)
            setLeftChild(p, r);
        else
            setRightChild(p, r);
    }
}

Note: BST stands for binary search tree.

The getInfo method of the ObjectTreeNode class:

private Object info;
public Object getInfo() {
    return info;
}

and finally, I don't know if these will help, but my TreeComparable compareTo declaration:

int compareTo(Object o);

and the compareTo method in the (Word) class:

String word;        
public int compareTo(Object o) {
    Word w = (Word) o;
    return this.word.compareTo(w.getWord());
}

The Help is greatly appreciated.

6
  • Welcome to Stack Overflow! Please take a tour. And if you can, try to put together a sscce. Commented Aug 10, 2014 at 12:18
  • 1
    getInfo is returning a String. A String is not TreeComparable. Makes no difference that a String is Comparable -- that does not make it TreeComparable. Commented Aug 10, 2014 at 12:54
  • So, even though the the class implementing getInfo only has Object variables it returns a string? I though upcasting was done automatically, and down casting was not. Is this where I am mistaken? Commented Aug 10, 2014 at 18:53
  • A String is an Object, so a method that is defined to return Object can return a String. Now, if you defined getInfo to return a TreeComparable then it could not return a String,. Commented Aug 10, 2014 at 19:21
  • And casting an object reference does not change the class of the referenced object. Unlike casting, eg, float with (int), which actually modifies the value cast, casting an object reference modifies nothing. Commented Aug 10, 2014 at 19:23

1 Answer 1

1

This is because String does not implement the interface TreeComparable. There is a an interface Comparable that String implements. String can be upcasted to this interface.

Sign up to request clarification or add additional context in comments.

1 Comment

So, how do I fix it using the TreeComparable interface, and the Word class implementing the compareTo function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.