Results 1 to 6 of 6
  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Why is my Comparator Crashing?

    Why is my following Comparator Crashing? The Error Messages are below the code. Thanks for any input!

    Java Code:
    import java.util.Comparator;
    import java.util.Map;
    
    
    public class SortByStyle implements Comparator<Furniture>{
        
        Map theMapToSort;
    
        public SortByStyle(Map theMapToSort) {
            this.theMapToSort = theMapToSort;
        }
    
        public int compare(Furniture key1, Furniture key2) {
            
            Furniture furn = new Furniture();
            Furniture furn2 = new Furniture();
            
            furn = (Furniture) theMapToSort.get(key1.getStyle());
            furn2 = (Furniture) theMapToSort.get(key2.getStyle());
            
            if(furn.getStyle().compareToIgnoreCase(furn2.getStyle()) < 0)
            {
                return -1;
            }
            
            else{
                return 1;
            }
    
        }
    }
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to project1.client.Furniture
    	at project1.client.SortByStyle.compare(SortByStyle.java:15)
    	at java.util.TreeMap.compare(TreeMap.java:1188)
    	at java.util.TreeMap.put(TreeMap.java:531)
    	at java.util.AbstractMap.putAll(AbstractMap.java:273)
    	at java.util.TreeMap.putAll(TreeMap.java:322)
    	at project1.client.Project1$ButtonListener2.actionPerformed(Project1.java:467)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6504)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Why is my Comparator Crashing?

    java.lang.Integer cannot be cast to project1.client.Furniture
    Which line in your code tries to do this?

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why is my Comparator Crashing?

    According to the line numbering above it is line 5. The first line of the class.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Why is my Comparator Crashing?

    A few things I notice: First you should be using generic type parameters when using maps (or any other collection for that matter).

    Next, the compare method should not know anything of the collection it is working on. Instead it should determine which of two items are larger.

    Look here for an example of how to implement this: Object Ordering (The Java™ Tutorials > Collections > Interfaces)

  5. #5
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why is my Comparator Crashing?

    I'm not sure how else I can make comparisons based upon class variables. The sort needs to be done alphabetically, in this case based upon String style, which is a member of the Furniture class.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Why is my Comparator Crashing?

    Abstract out the whole idea of using a map. Simply provide a comparator which has a method which takes two Furniture objects and returns the string comparison of them. The String class already has a comparison which returns a proper value, make use of it.

    Java Code:
    public int compare(Furniture f1, Furniture f2){
      String f1Style = ...;
      String f2Style = ...;
      return f1 style compared to f2 style
    }

Similar Threads

  1. Method crashing
    By TyCox94 in forum AWT / Swing
    Replies: 2
    Last Post: 09-25-2011, 05:01 AM
  2. Crashing code
    By ke5awf in forum Advanced Java
    Replies: 2
    Last Post: 08-23-2011, 10:27 AM
  3. [Help please] java keeps crashing
    By BoredomKills in forum New To Java
    Replies: 0
    Last Post: 01-11-2011, 02:38 AM
  4. the eclipse is crashing
    By miko5054 in forum Eclipse
    Replies: 1
    Last Post: 06-07-2010, 07:11 PM
  5. While loop crashing?
    By ToplessGrunt in forum New To Java
    Replies: 6
    Last Post: 10-20-2008, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •