1 2 Previous Next 26 Replies Latest reply: Oct 28, 2012 5:53 AM by ptoye RSS

    Illegal Argument exception within Swing

    ptoye
      I've written a Swing-based program. When I start it there's an Illegal Argument exception with the message "Comparison method violates its general contract".

      But the stack doesn't have any of my code in it - it's entirely on the AWT event queue (see copy of stack below). And there are no comparators in my program at all.

      The program appears to run OK, but it's more than a bit disconcerting. Anyone here got any ideas what's going wrong inside Swing?

      Thanks in advance,

      Peter

      Stack output is:

      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)
        • 1. Re: Illegal Argument exception within Swing
          TPD-Opitz
          Find all your Classes implementing <tt>Comparable</tt> interface.
          Do they fullfill the restriction given in
          http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html?

          This classes should not only implement <tt>compareTo(T other)</tt> but also <tt>equals</tt> (and therefore <tt>hashcode()</tt> too).

          bye
          TPD
          • 2. Re: Illegal Argument exception within Swing
            Kayaman
            ptoye wrote:
            But the stack doesn't have any of my code in it
            It may be sorting instances of your classes that have fudged the general contract.

            If you have any JComponent subclasses, check that their equals and hashcode methods are correctly implemented.
            • 3. Re: Illegal Argument exception within Swing
              ptoye
              Kayaman wrote:
              If you have any JComponent subclasses, check that their equals and hashcode methods are correctly implemented.
              I have only one such class, which extends JComponent. I've not overridden either equals or hashcode as there doesn't seem to be much point. Or is there a subtle reason why I should write my own?

              There's also a class which extends JFrame, of course, to give me a window.

              Edited by: ptoye on Oct 23, 2012 9:12 AM

              Edited by: ptoye on Oct 23, 2012 9:16 AM

              Edited by: ptoye on Oct 23, 2012 9:17 AM
              • 4. Re: Illegal Argument exception within Swing
                ptoye
                TPD Opitz-Consulting com wrote:
                Find all your Classes implementing <tt>Comparable</tt> interface.
                Do they fullfill the restriction given in
                http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html?

                This classes should not only implement <tt>compareTo(T other)</tt> but also <tt>equals</tt> (and therefore <tt>hashcode()</tt> too).

                bye
                TPD
                I don't have any classes which directly implement Comparable. Only an extension of JComponent - see my other comment for details.

                Edited by: ptoye on Oct 23, 2012 9:13 AM
                • 5. Re: Illegal Argument exception within Swing
                  DrClap
                  Then to see if that class is somehow the problem, have it implement the method required by Comparable trivially:
                  public int compareTo(Object other) {
                    return super.compareTo(other);
                  }
                  This won't change the behaviour of your code, but it might make your class show up in the stack trace.
                  • 6. Re: Illegal Argument exception within Swing
                    abillconsl
                    "the stack doesn't have any of my code in it "

                    What about this: java.util.TimSort.mergeLo(TimSort.java:747) ?
                    • 7. Re: Illegal Argument exception within Swing
                      ptoye
                      Thanks Dr. Clap.

                      But JComponent (and its supers) don't implement CompareTo. Overriding the Equals and HashCode methods doesn't change anything.
                      • 8. Re: Illegal Argument exception within Swing
                        ptoye
                        abillconsl wrote:

                        What about this: java.util.TimSort.mergeLo(TimSort.java:747) ?
                        Err.... That's part of the Java run-time. java.util is supplied by Java, not me!

                        Edited by: ptoye on Oct 23, 2012 11:04 AM
                        • 9. Re: Illegal Argument exception within Swing
                          abillconsl
                          Yes that's very true. I saw TimSort and well ... what version of Java are you running? I never noticed TimSort before, I am running v1.6.31
                          • 10. Re: Illegal Argument exception within Swing
                            DrClap
                            ptoye wrote:
                            But JComponent (and its supers) don't implement CompareTo. Overriding the Equals and HashCode methods doesn't change anything.
                            No, you're correct, that's apparent to anybody who looks at the API. (Which I only just did now.) Then what is it which SortingFocusTraversalPolicy is sorting? Maybe it's time to have a look into the source code (src.zip in your JDK download). It's not impossible that the bug is in Oracle's code.
                            • 11. Re: Illegal Argument exception within Swing
                              doremifasollatido
                              javax.swing.LayoutComparator seems to be the default comparator for the javax.swing.LayoutFocusTraversalPolicy, and LayoutComparator doesn't define equals. This seems to be a bug in Java 7---they didn't define equals in LayoutComparator, so sorting now fails.

                              http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source says:
                              Area: API: Utilities
                              Synopsis: Updated sort behavior for Arrays and Collections may throw an IllegalArgumentException
                              Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an
                              IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation.
                              If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.
                              Nature of Incompatibility: behavioral
                              RFE: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6804124
                              Note the system property mentioned in that description that you can use to get the pre-Java7 behavior.

                              Indeed, this bug has already been reported:
                              http://webcache.googleusercontent.com/search?q=cache:QDf6QjnHLS8J:bugs.sun.com/bugdatabase/view_bug.do%3Fbug_id%3D7176968+&cd=12&hl=en&ct=clnk&gl=us
                              • 12. Re: Illegal Argument exception within Swing
                                ptoye
                                abillconsl wrote:
                                what version of Java are you running? I never noticed TimSort before, I am running v1.6.31
                                Version 7. Maybe I shouldn't have upgraded. Oracle seem to have put all sorts of bugs into 7.
                                • 13. Re: Illegal Argument exception within Swing
                                  ptoye
                                  doremifasollatido wrote:
                                  javax.swing.LayoutComparator seems to be the default comparator for the javax.swing.LayoutFocusTraversalPolicy, and LayoutComparator doesn't define equals. This seems to be a bug in Java 7---they didn't define equals in LayoutComparator, so sorting now fails.

                                  Indeed, this bug has already been reported:
                                  http://webcache.googleusercontent.com/search?q=cache:QDf6QjnHLS8J:bugs.sun.com/bugdatabase/view_bug.do%3Fbug_id%3D7176968+&cd=12&hl=en&ct=clnk&gl=us
                                  Thanks. That bug (7176968) seems to have disappeared from the Oracle bug database. I begin to get the feeling that Oracle are removing adverse comments. I wonder why?

                                  [later] I tried setting the property. The article you reference doesn't tell me what to set it to. So I tried null (not allowed), "0" and "1" both of which don't change anything. All a bit dispiriting really.

                                  Edited by: ptoye on Oct 24, 2012 2:12 AM - results of test
                                  • 14. Re: Illegal Argument exception within Swing
                                    ptoye
                                    DrClap wrote:

                                    No, you're correct, that's apparent to anybody who looks at the API. (Which I only just did now.) Then what is it which SortingFocusTraversalPolicy is sorting? Maybe it's time to have a look into the source code (src.zip in your JDK download). It's not impossible that the bug is in Oracle's code.
                                    This seems to be the current opinion.

                                    Oracle change something, don't test it and get surprised and defensive when people complain. The way of the world.
                                    1 2 Previous Next