Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Comparison method violates its general contract!" - TimSort and GridLayout

I made a color palette with a jPanel and a JLabel array in it. At first it worked well, but then i put some other jLabels out of the JPanel and added them some events. Now I keep getting this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeLo(TimSort.java:747) at java.util.TimSort.mergeAt(TimSort.java:483) at java.util.TimSort.mergeCollapse(TimSort.java:410) at java.util.TimSort.sort(TimSort.java:214) at java.util.TimSort.sort(TimSort.java:173) at java.util.Arrays.sort(Arrays.java:659) at java.util.Collections.sort(Collections.java:217) at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136) at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110) at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:435) at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166) at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:515) at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:380) at java.awt.Component.dispatchEventImpl(Component.java:4731) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) 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:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.SequencedEvent.dispatch(SequencedEvent.java:116) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:682) at java.awt.EventQueue$3.run(EventQueue.java:680) 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:696) at java.awt.EventQueue$4.run(EventQueue.java:694) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:693) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 

I tried to remove everything i've done after first time i got this error, but still keep getting it. When i change the layout from GridLayout to anything else, then the error disappears, but the code becomes useless. So i need GridLayout. When i move everything in that JPanel to another JPanel, the error also goes away. But when i remove the first JPanel, error comes back.

By the way, the program works, but it's not pleasent to keep getting errors...

Edit: When i use less than 225 color, there's no error. I'm really curious about what's happening. Any explanation would be appreciated...

like image 334
s.alem Avatar asked Nov 26 '12 23:11

s.alem


2 Answers

It seems to me like you've hit a bug in the JDK since the error seems to come from Swing classes.

Options:

  1. Define the property java.util.Arrays.useLegacyMergeSort as true. Either using in your code the line

    System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 

    before any Swing code. As the first line in the main method should work.

    Or adding

    -Djava.util.Arrays.useLegacyMergeSort=true 

    to your starting options (in the console, or in the project properties in an IDE, Ant script, etc.)

  2. Upgrade your JDK and see if the problem goes away

  3. Downgrade to Java 6
like image 123
madth3 Avatar answered Oct 15 '22 00:10

madth3


Report my findings:

-Djava.util.Arrays.useLegacyMergeSort=true 

works

but

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true"); 

does not work.

It is due to the fact that in JDK Arrays.class

 static final class LegacyMergeSort {     private static final boolean userRequested = ... 

It is a static variable which is defined when jvm starts. Setting System property in the program will have no effect if the class has been loaded into jvm.

I have beeing monitoring the LegacyMergeSort.userRequested variable, and the findings confirmed with above statement.

Update: The program must set system properties before java.util.Arrays is loaded to classloader. Otherwise, once it is loaded, setting the properties is not going to be useful due to the reason mentioned above.

Make sure nothing else loaded Arrays.class:

By putting following code to your program to test:

    java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[] { String.class });     m.setAccessible(true);     ClassLoader cl = ClassLoader.getSystemClassLoader();     Object test1 = m.invoke(cl, "java.util.Arrays");     System.out.println("test1 loaded? ->" + (test1 != null)); 
like image 23
Robin Loxley Avatar answered Oct 14 '22 23:10

Robin Loxley