Results 1 to 6 of 6
Thread: Why is my Comparator Crashing?
- 10-05-2011, 03:25 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
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)
- 10-05-2011, 03:33 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Why is my Comparator Crashing?
Which line in your code tries to do this?java.lang.Integer cannot be cast to project1.client.Furniture
- 10-05-2011, 03:37 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
Re: Why is my Comparator Crashing?
According to the line numbering above it is line 5. The first line of the class.
- 10-05-2011, 04:01 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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)
- 10-05-2011, 04:42 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 46
- Rep Power
- 0
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.
- 10-05-2011, 04:51 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
-
Method crashing
By TyCox94 in forum AWT / SwingReplies: 2Last Post: 09-25-2011, 05:01 AM -
Crashing code
By ke5awf in forum Advanced JavaReplies: 2Last Post: 08-23-2011, 10:27 AM -
[Help please] java keeps crashing
By BoredomKills in forum New To JavaReplies: 0Last Post: 01-11-2011, 02:38 AM -
the eclipse is crashing
By miko5054 in forum EclipseReplies: 1Last Post: 06-07-2010, 07:11 PM -
While loop crashing?
By ToplessGrunt in forum New To JavaReplies: 6Last Post: 10-20-2008, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks