java.lang.ArrayIndexOutOfBoundsException: No such child: 0 in JMEDesktop.paint()

Hi,



I am periodically, more or less randomly, getting the following stack trace on System.err:



    [java] Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: No such child: 0

    [java]    at java.awt.Container.getComponent(Container.java:280)

    [java]    at javax.swing.JComponent.rectangleIsObscured(JComponent.java:4090)

    [java]    at javax.swing.JComponent.paint(JComponent.java:1001)

    [java]    at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)

    [java]    at com.jmex.awt.swingui.JMEDesktop$2.paint(Unknown Source)

    [java]    at javax.swing.JComponent._paintImmediately(JComponent.java:4836)

    [java]    at javax.swing.JComponent.paintImmediately(JComponent.java:4633)

    [java]    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)

    [java]    at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)

    [java]    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

    [java]    at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)

    [java]    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)

    [java]    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

    [java]    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

    [java]    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

    [java]    at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



I guess it is a bug of some kind in JMEDesktop, but I don't know enough about Swing/AWT to try to troubleshoot it on my own. One thing that we're doing with JMEDesktop that I suppose could be a bit iffy include:



if ( displayTweak ) {

jmeDesktop.getJDesktop().add(tweakFrame);

}

else {

jmeDesktop.getJDesktop().remove(tweakFrame);

}



in order to toggle the display of a particular frame. It's hard to tell exactly, but I have a feeling that the exception happens most often when the tweakFrame is hidden (removed), and it then means that the part of another JMEDesktop frame that lies underneath it is cut out, showing the 3D background.



Does anybody have an idea of how to solve this?

This might not be the solution, but in any case you should make sure that any adding or removing or for that matter any other type of Swing methods are called from the Swing thread only. Swing has a utility class for this (SwingUtilities) with methods invokeAndWait and invokeLater. For an example of how to do this, JMEDesktop.java uses them as well. This is not a jME issue, it's how Swing must be used in any application, as specified in the documentation of Swing.



If the exception occurs right at the moment when you remove the tweakFrame, and you are currently calling these methods from the jME thread, this is likely the cause.

Thanks for the tip! After a couple of initial tests, that seems to have helped. Can't be sure yet, but I used to get a couple of those exceptions in a test run of a few minutes, so it looks good.  :slight_smile: