Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ClassCastException with Point2D.Double

807607Oct 29 2006 — edited Oct 29 2006
Hi,
I just ran in an unexpected problem. Consider the code below:
import java.awt.geom.*;
//import java.awt.geom.Point2D;
//import java.awt.geom.Point2D.Double;

. . .

    public static final Comparator X_VALUE_COMPARATOR = new Comparator() {               
        public int compare(Object o1, Object o2) {
            java.awt.geom.Point2D.Double obj1 = (java.awt.geom.Point2D.Double)o1;
            java.awt.geom.Point2D.Double obj2 = (java.awt.geom.Point2D.Double)o2;   // ClassCastException
            double diff = obj1.getX() - obj2.getX();
            return((int)Math.signum(diff));
            //return 0;
        }
    };
This is a fragment from a class that imports the mentioned packages and contains this comparator. The second cast (with obj2) throws a ClassCastException, because he tries to cast to java.lang.Double. Surprisingly to me, the line before is no problem. If I comment out the line with obj2 and the following dependent ones, it executes with no problems.

Is this a bug? It seems to be similar to
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4198349
but the workaround doesn't work for me. As you see, I'm using the full classpath and have also tried several other combinations.
I am using JDK 1.5 on Mac OS X.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 26 2006
Added on Oct 29 2006
9 comments
313 views