Threading error

Hi all,

I started with the Mahjonng app as an example and built a simple application with multiple menu states that all derive from JMEDesktopState.  Things worked alright but I changed my app to have more of a real state-machine controlling the flow, and I created a static ClassFactory to create the menu states instead of having them all created as part of the Main.  So I've gotten past that restructuring, everything compiles, and it looks like it runs.  My first menu state (MainMenu) is displayed, a bunch of buttons such as "start campaign", "edit settings", "quit".  As soon as I press a button I get a threading error and I did a quick search, but I haven't been able to find anything that seems to relate to the problem.  Can anyone point me to a source of more info abou the different threads that are running and how to work with them, or (even better) think of any known rules that I violated?



The console output is:

Aug 23, 2008 8:18:26 PM com.jme.input.joystick.DummyJoystickInput <init>

INFO: Joystick support is disabled

Aug 23, 2008 8:18:26 PM com.jme.system.lwjgl.LWJGLDisplaySystem <init>

INFO: LWJGL Display System created.

Aug 23, 2008 8:18:26 PM com.jme.renderer.lwjgl.LWJGLRenderer <init>

INFO: LWJGLRenderer created. W:  800H: 600

Aug 23, 2008 8:18:27 PM com.jme.renderer.AbstractCamera <init>

INFO: Camera created.

Aug 23, 2008 8:18:27 PM com.jmex.audio.openal.OpenALSystem setupSourcePool

INFO: max source channels: 64

Aug 23, 2008 8:18:27 PM com.jmex.game.state.GameStateManager create

INFO: Created GameStateManager

Aug 23, 2008 8:18:27 PM com.jme.util.lwjgl.LWJGLTimer <init>

INFO: Timer resolution: 1000 ticks per second

Aug 23, 2008 8:18:27 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:27 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:27 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:27 PM com.jme.scene.Node attachChild

INFO: Child (Desktop) attached to this node (GUI)

Aug 23, 2008 8:18:27 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:27 PM com.jme.scene.Node attachChild

INFO: Child (my box) attached to this node (background: RootNode)

Aug 23, 2008 8:18:30 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:30 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:30 PM com.jme.scene.Node <init>

INFO: Node created.

Aug 23, 2008 8:18:30 PM class com.jmex.awt.swingui.JMEDesktop onButton(swingButton, pressed, x, y)

SEVERE: Exception

java.lang.reflect.InvocationTargetException

at java.awt.EventQueue.invokeAndWait(Unknown Source)

at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)

at com.jmex.awt.swingui.JMEDesktop.onButton(JMEDesktop.java:452)

at com.jmex.awt.swingui.JMEDesktop$ButtonAction.performAction(JMEDesktop.java:1174)

at com.jme.input.ActionTrigger.performAction(ActionTrigger.java:264)

at com.jme.input.InputHandler.processTriggers(InputHandler.java:426)

at com.jme.input.InputHandler.update(InputHandler.java:411)

at com.jme.input.InputHandler.updateAttachedHandlers(InputHandler.java:440)

at com.jme.input.InputHandler.update(InputHandler.java:412)

at com.jmex.awt.swingui.JMEDesktopState.update(JMEDesktopState.java:126)

at com.jmex.game.state.GameStateNode.update(GameStateNode.java:71)

at com.jmex.game.StandardGame.update(StandardGame.java:353)

at com.jmex.game.StandardGame.run(StandardGame.java:225)

at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread

at java.awt.EventQueue.invokeAndWait(Unknown Source)

at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)

at com.jmex.awt.swingui.JMEDesktopState.init(JMEDesktopState.java:97)

at com.jmex.awt.swingui.JMEDesktopState.<init>(JMEDesktopState.java:77)

at heavylead.menu.swingui.SwingMenuGameState.<init>(SwingMenuGameState.java:15)

at heavylead.menu.swingui.SelectCampaignState.<init>(SelectCampaignState.java:73)

at heavylead.ClassFactory.getSelectCampaignState(ClassFactory.java:90)

at heavylead.menu.swingui.MainMenuState$1.actionPerformed(MainMenuState.java:53)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at com.jmex.awt.swingui.JMEDesktop.dispatchEvent(JMEDesktop.java:671)

at com.jmex.awt.swingui.JMEDesktop.sendAWTMouseEvent(JMEDesktop.java:846)

at com.jmex.awt.swingui.JMEDesktop.access$11(JMEDesktop.java:778)

at com.jmex.awt.swingui.JMEDesktop$7.run(JMEDesktop.java:454)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Aug 23, 2008 8:18:32 PM com.jme.scene.Node detachChildAt

INFO: Child removed.





Thanks,

  Mike

OK, I acted on a hunch and I found a work-around, but not really a solution.



Imagine there are two states: MainMenu and SelectCampaign.  The original version of the program created both states and then displayed MainMenu.  In the changes I made, I went to a "lazy invocation" model, where I would create it only when it was first referenced.  Thus, I would create MainMenu, display it, and when the user hits the button, I would create and display the SelectCampaign state.  I changed my class factory to create everything up-front instead of lazy, and everything works again.  But I still don't have a real good idea of exactly what I did wrong, or how to prevent myself from doing it again in the future.  Any ideas?



Thanks again,

  Mike