Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wanted to create a compareTo method but it gives me an error. Can anyone help me with this ?

     package defaultpackage;
     public class HuffmanEntry implements Entry<Integer, String> {
     private String c;  
     private Integer freq;

     public HuffmanEntry(String c, Integer freq){    
     this.c = c;
     this.freq = freq;
     }

     @Override
     public int compareTo(Integer arg0) {
    if(getKey()<arg0){
        return -1;
    }else if(getKey()>arg0){
        return 1;
    }else{
        return 0;   
    }
}

      @Override
      public Integer getKey() {
      return freq;
      }

Here's my error:

Exception in thread "main" java.lang.ClassCastException: defaultpackage.HuffmanEntry cannot be
cast to java.lang.Integer
at defaultpackage.HuffmanEntry.compareTo(HuffmanEntry.java:1)
share|improve this question
    
Why does your class attempt to implement comparison with integers, rather than with other instances of the same class? –  user2357112 Mar 21 at 1:09
    
Could you show us Entry? What is its compareTo signature? –  Makoto Mar 21 at 1:09
    
@Override public int compareTo(Integer arg0) { if(getKey()<arg0){ return -1; }else if(getKey()>arg0){ return 1; }else{ return 0; } –  guilhebl Mar 21 at 1:11
    
package defaultpackage; public interface Entry<K,V> extends Comparable<K>{ K getKey(); V getValue(); void setKey(K key); void setValue(V Value); } This is my Entry. –  user3444455 Mar 21 at 1:21

2 Answers 2

up vote 1 down vote accepted

There are serious problems with the quality of the "evidence" you have provided us:

  1. The stacktrace seems to say that the exception is thrown at line 1 of HuffmanEntry.java, and that is impossible.

  2. The stacktrace seems to say that your code is attempting to cast a HuffmanEntry object to an Integer in compareTo, but (according to the source code) that cannot happen in that method. All objects used in that method have a declared type of Integer, and there are no explicity typecasts.

  3. The stacktrace shows that compareTo is the one and only method call, and that is impossible.

(3 impossible things before lunch? Nah! Don't buy it!)

In short, the evidence you have shown us is not real. It has been tweaked / mangled, and in ways that make it impossible to interpret.

Please provide a real (complete and unedited) stacktrace, and source code that precisely matches the compiled code you ran to produce the stacktrace. Also any compilation errors.

Alternatively, provide us with an SSCCE that we can run ourselves to reproduce this facially impossible behaviour / evidence.


Looking at the code you linked to, I think I understand the general problem ... if not the specific one.

Basically, you are mixing generic and non-generic code and (you must be) ignoring the resulting compiler warnings about unchecked conversions, use of raw types and so on. The result is that errors that should be picked up by the Java compile-time type checker are not being reported.

For example, in MyHeap.java, you write:

private ArrayList<Entry> entryList; 

But Entry is actually a generic interface, meaning that ArrayList is actually being interpreted as ArrayList<Entry<Object, Object>> where Object is the raw type corresponding to the unbounded type parameters K and T. Now there is an int compareTo(K) method in the Entry interface ... and the raw type signature is int compareTo(Object).

Now look at

if (entryList.get(i*2).compareTo(entryList.get(i*2+1))==-1){ 

Note that you are effectively calling Entry.compareTO(Object) passing another Entry as the argument. The compiler has said OK .... because it thinks you are using raw types at that point. But if you had been using generics properly, the compiler would have realized that you are calling Entry<K, V>.compareTo(Entry<K, V>) where the generic method signature is really Entry<K, V>.compareTo(V) . That would have been flagged as a compilation error.

I suspect that this explains the strange exception too. The Java compiler has added some hidden runtime typecasts to make sure that the JVM's core runtime type safety is not compromised. You are seeing the exception when one of those runtime checks fails. And that happens because you effectively ignored or suppressed the compile time checks that should have you told you of your programming error.

Anyway, the bottom line is that you should NOT ignore compiler warnings about misuse of generics, unchecked conversions, etc. You do so at your peril ... and in this case, you got bitten.

share|improve this answer
    
If you want to see all of my work. Here is it.link I think I've done most of the work. –  user3444455 Mar 21 at 2:07
    
Thanks. I found my mistake. I missused the compareTo method, because I used it before defining the method, I forgot looking it again after implementing the method. Before it was compareTo(entryList.get(i*2)) now I changed to this-> compareTo((Integer)entryList.get(i*2).getKey() –  user3444455 Mar 21 at 11:45
    
@user3444455 - But the compiler should have told you that ... assuming you were paying attention to warnings. –  Stephen C Mar 21 at 12:18

You implement Comparable to allow your class objects to be compared, not to compare different objects.

Perhaps you want to provide use something like if (Integer x > huffmanObject.getFreq()) instead? Sounds much simpler.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.