Skip to main content

Comparison method violates its general contract!

22 replies [Last post]
ixanos
Offline
Joined: 2008-12-06
Points: 0

An application that was running with java 1.6 has been compiled with java 1.7.0 build 80. Now I keep getting this error message:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:868)
at java.util.TimSort.mergeAt(TimSort.java:485)
at java.util.TimSort.mergeForceCollapse(TimSort.java:426)
at java.util.TimSort.sort(TimSort.java:223)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:653)
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 javax.swing.JComponent.requestDefaultFocus(JComponent.java:2618)
at sun.swing.SwingUtilities2.tabbedPaneChangeFocusTo(SwingUtilities2.java:1566)
at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:398)
at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
at javax.swing.JTabbedPane.setSelectedIndex(JTabbedPane.java:591)
at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(BasicTabbedPaneUI.java:3637)
at java.awt.Component.processMouseEvent(Component.java:6427)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6195)
at java.awt.Container.processEvent(Container.java:2203)
at java.awt.Component.dispatchEventImpl(Component.java:4790)
at java.awt.Container.dispatchEventImpl(Container.java:2261)
at java.awt.Component.dispatchEvent(Component.java:4616)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4800)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4460)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4393)
at java.awt.Container.dispatchEventImpl(Container.java:2247)
at java.awt.Window.dispatchEventImpl(Window.java:2671)
at java.awt.Component.dispatchEvent(Component.java:4616)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:651)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:255)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:170)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:155)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:136)

Prior to getting this error message i notice the firing of this event:

java.awt.event.HierarchyEvent[HIERARCHY_CHANGED (SHOWING_CHANGED,javax.swing.JPanel[,2,25,1275x659,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.CompoundBorder@1fd245f,flags=9,maximumSize=,minimumSize=,preferredSize=],javax.swing.JTabbedPane[,0,0,1280x687,invalid,layout=javax.swing.plaf.basic.BasicTabbedPaneUI$TabbedPaneScrollLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=353,maximumSize=,minimumSize=,preferredSize=,haveRegistered=false,tabPlacement=TOP])] on javax.swing.JPanel[,2,25,1275x659,layout=java.awt.BorderLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.CompoundBorder@1fd245f,flags=9,maximumSize=,minimumSize=,preferredSize=]

Any ideas? I assume this is a bug as I have not changed anything and this error is not thrown with java 1.6.0

cordially
ioannis

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
thetan
Offline
Joined: 2004-08-19
Points: 0

Please post the code.

ixanos
Offline
Joined: 2008-12-06
Points: 0

Dear antontarasov
Yes the error is reproduced then. I havent doen the test as you indicated, because I recall firstly seeing this error when I indeed had deactivated all components in the panel.
cheers
ixanos

antontarasov
Offline
Joined: 2008-06-11
Points: 0

Thanks, ixanos. The source of the problem is detected, I suppose. We will think of the solution...

Regards,
Anton.

jmehrens
Offline
Joined: 2005-06-14
Points: 0

> "Has the same bug that was solved in 2004 reappeared ?"
The source of the problem was identified, LayoutComparator has a transitivity problem but, the patch was applied to the SortingFocusTraversalPolicy.

Then enters, http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6804124
and the orginal problem comes back in a new form.

If you run your original code with system property 'java.util.Arrays.useLegacyMergeSort=true', I bet you would not see the issue. However, TimSort is not to blame, it is just calling out that the comparator used is broken. Which is explained in detail in 5070991.

Jason

antontarasov
Offline
Joined: 2008-06-11
Points: 0

Hi Jason,

Thanks for pointing to 6804124. New bug has been filed -
6804124 "swing LayoutComparator breaks transitivity that may lead to IllegalArgumentException"

Anton.

antontarasov
Offline
Joined: 2008-06-11
Points: 0

Oh, I typed wrong bug number =(
Here is the new bug:

6923200 "swing LayoutComparator breaks transitivity that may lead to IllegalArgumentException"

Sorry.

antontarasov
Offline
Joined: 2008-06-11
Points: 0

Hi ixanos,

Thanks for reporting the issue. May I ask you to perform some more testing?

Namely, could you please try to put the content of the tab where you face the problem
in, say, JPanel? That is, remove JTabbedPane from the window at all, just a JPanel with
the content.

Now, try to activate/deactivate (switch to some other window and back) your window.
Does it reproduce the issue?

Thanks,
Anton.

ixanos
Offline
Joined: 2008-12-06
Points: 0

Dear friends

I managed to solve this issue.
I dont exactly know why the error was thrown but know how to avoid this error.
I avoided it by writing a custom FocusTraversalPolicy for each panel of each Tab in my application's JTabbedPane. Once the custom FocusTraversalPolicy was in place and active all errors dissappeared. It is also worth mentioning that with java 1.6 these errros where not appearing. This means that java 1.7 is stricter which I welcome warmly as it helps us building robuster applications!

(*) I would like to personally thank [jmehrens] for mentioning the existing bug reports as they guided me to understanding that the FocusTraversalPolicy could be the root of the issue.

cheers
ixanos

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello ixanos

Thanks for your reports!

We are working on this problem, this bug will certainly be fixed for JDK 1.7

Thanks
alexp

ixanos
Offline
Joined: 2008-12-06
Points: 0

I am using Java 1.7 build 81 and not java 1.5.
Has the same bug that was solved in 2004 reappeared ?

jmehrens
Offline
Joined: 2005-06-14
Points: 0

The LayoutComparator is broken as described here:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5070991

Jason

ixanos
Offline
Joined: 2008-12-06
Points: 0

Also I noticed that when using the Metal Look and Feel and when the error occurs the target tab of the JTabbedPane remains unselected (not highlighted). As opposed to the NimbusLookAndFeel where the target tab of the JTabbedPane does indeed get highlighted. So actually, in the NimbusLookAndFeel the program continues undisturbed. Though , it is not pleasant to see errors thrown!

Hope this helps the debug process.

Cheers
ixanos

ixanos
Offline
Joined: 2008-12-06
Points: 0

One issue i can see in the prodcued excpetion trace, is that java 1.7 uses deprecated methods like [u][b][i]JComponent.requestDefaultFocus()[/i][/b][/u]. This method is deprecated as of Java 1.4 ! I havent checked all other methods in the Trace, but I am sure this needs to be changed! Hope everyone agrees on this.

Perhaps this is the cause anyhow.

cheers
ixanos

ixanos
Offline
Joined: 2008-12-06
Points: 0

The error is reproducable in all Look And Feels.

("javax.swing.plaf.metal.MetalLookAndFeel");
("javax.swing.plaf.nimbus.NimbusLookAndFeel");
("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");("javax.swing.plaf.metal.MetalLookAndFeel");

Unfortunately, it is not reproducable in SwingSet2 nor on a empty application containing the very same JTabbedPane. I've checked both.

then again the error occurs in different tabs in different Look And Feels. So I cannot really tell when and which tab will throw this error!

Any ideas will be very appreciated.

Cordially
ixanos

ixanos
Offline
Joined: 2008-12-06
Points: 0

the same error appears also when I cascade disable a panel containing a JTree. The error occurs when the recursive method reaches the JTree component and attempts to disable it using setEnabled(false);

---DEACTIVATING---
START PANEL [31321506]: [null][javax.swing.JViewport][javax.swing.JViewport]
--A3---[javax.swing.JTree]
---DEACTIVATING---
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:868)
at java.util.TimSort.mergeAt(TimSort.java:485)
at java.util.TimSort.mergeCollapse(TimSort.java:408)
at java.util.TimSort.sort(TimSort.java:214)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:653)
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.getComponentAfter(SortingFocusTraversalPolicy.java:280)
at javax.swing.LayoutFocusTraversalPolicy.getComponentAfter(LayoutFocusTraversalPolicy.java:106)
at java.awt.Component.getNextFocusCandidate(Component.java:7785)
at java.awt.Component.transferFocus(Component.java:7753)
at java.awt.Component.disable(Component.java:1495)
at javax.swing.JComponent.disable(JComponent.java:3635)
at java.awt.Component.enable(Component.java:1473)
at java.awt.Component.setEnabled(Component.java:1436)
at javax.swing.JComponent.setEnabled(JComponent.java:2677)

ixanos
Offline
Joined: 2008-12-06
Points: 0

further on, I see that when switching to tabs of the JTabbedPane where the error occurs the highlighting of the clicked tab does not happen, though the application has switched to the clicked tab. In other words: Lets say the application has 12 tabs. When it starts it defaults to tab 1. when I click to tab 7 the error occurs. There are tabs where this error does not happen. So clicking on a tab where the error does not happen, visually two things happen:
1. the new tab is highlighted
2. the new tab's components are shown
In case I click on a tab where the error happens only this happens:
1. the new tab's components are shown
but the tab does not get highlighted. If I minimize and reshow the whole application (force a repaint) the tab gets highlighted.

Perhaps this helps to localize the error (in or before the repaint of the JTabbedPane ??).

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Could you please try to create a small test case with a JTabbedPane
that reminds your application?

Some other questions:
What LookAndFeel do you use, is it reproducible with any other LaFs?
Is it reproducible on SwingSet2 demo application (bundled with any JDKs)

Thanks
alexp

ixanos
Offline
Joined: 2008-12-06
Points: 0

I can assure you the problem is reproducible.
It occurs when i switch among JTabbedPane tabs.
If I change the tab-layout piolicy then the error occurs when i click on other tabs. Therefore I safely assume it has not anything to do with the actual contents of the tabs of the JTabbedPane.
Then again I just completed a new copy of the application without any contents in the actual panes and cannot reproduce the error.
But as i said in the full-blown application the error is always and all times reproducable.
Perhaps its an error in the TimSort.mergeHi() algorithm and it is only reproducable when a number of components exist. I assume this method needs to be retested.

ixanos
Offline
Joined: 2008-12-06
Points: 0

I find it difficult to understand which part of the code actually reproduces the error.
practically I see that when I start our application the error occurs when:

1. application is shown
2. the application is visualized within a JTabbedPane
3. I click on a specific tab of the JTabbedPane and the error occurs.

Any ideas how I can locate the peace of code where this happens?
I added a ChangeListener to the JTabbedPane but still I could not really locate the error.

Any ideas how to locate the error?

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello ixanos

The stack trace clearly says that the problem comes from JTabbedPane,
so you may try to create a small test case with a frame and a tabbedPane
which is set up like the one in your application and check it out.

One more thing to check - is this bug always reproducible
or it comes out from time to time?

Could you run your application on another computer and let me know if it reproducible there?

Thanks
alexp

alexfromsun
Offline
Joined: 2005-09-05
Points: 0

Hello ixanos

Hm... looks like a bug to me

Could you please provide a short test case for it?

Thanks
alexp

javaUser361
Offline
Joined: 2012-06-18
Points: 0

I am facing similar issue while using to JRE7. Code works fine in JRE6.

Sometimes while enabling disabling a button, i get following exception.

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeForceCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getComponentAfter(Unknown Source)
at javax.swing.LayoutFocusTraversalPolicy.getComponentAfter(Unknown Source)
at java.awt.Component.getNextFocusCandidate(Unknown Source)
at java.awt.Component.transferFocus(Unknown Source)
at java.awt.Component.disable(Unknown Source)
at javax.swing.JComponent.disable(Unknown Source)
at java.awt.Component.enable(Unknown Source)
at java.awt.Component.setEnabled(Unknown Source)
at javax.swing.JComponent.setEnabled(Unknown Source)
at javax.swing.AbstractButton.setEnabled(Unknown Source)

There is http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6923200 but in "State 11-Closed, Not Reproducible,

What is way to resolve this.