Mouse sometimes throws NoSuchElementException

Hello all.

I sometimes obtain NoSuchElementException exception:


java.util.NoSuchElementException
   at java.util.LinkedList.remove(LinkedList.java:788)
   at java.util.LinkedList.remove(LinkedList.java:357)
   at com.jmex.awt.input.AWTMouseInput.update(AWTMouseInput.java:195)
   at com.jme.input.InputSystem.update(InputSystem.java:67)
   at jme.BaseGame.start(BaseGame.java:84)



It's happen when BaseGame calls InputSystem.update();
In AWTMouseInput is loop for events handling and removing from queue:

            while ( !swingEvents.isEmpty() )
            {
                MouseEvent event = swingEvents.remove( 0 );



I not found any other occurence remove() function calling,
so I understand what can do it.
Anybody know why it can happens?

Thank you for help.

From what you've said (so correct me if I'm wrong), the problem is there are no elements in the linked list of swingEvents when the remove(0) is called.


while ( !swingEvents.isEmpty() )

   
This line implies that the remove should only be called when there are swingEvents, right? ..well, maybe.

A key piece of information is that the class LinkedList implementation is not synchronized.

This means a possible cause of this problem could be down to multithreaded accessing of the list.

List is accessed internally, user have no access to list so if you are right, it must be bug.

But what is strage that when this exception occurs, it is throwed every next update call.

Just for reference; I've never used AWTMouseInput, I tend to use customised versions of AbsoluteMouse and ThirdPersonMouseLook.



Having now looked at the source for the AWTMouseInput, I can safely say that I'm no closer to understanding the cause :frowning: One possible multithreaded scenario that could cause the problem would the one thread inside update() while another is adding to the list

This error occurs about one times per one, two days so I'm not able reproduce it.

I added action to input:


      input.addAction( new MouseInputAction()
      {
         @Override
         public void performAction(InputActionEvent e)
         {
            MouseInput mi = MouseInput.get();
            int wd = mi.getWheelDelta();
            ..........

Hmmm…



Unfortunately the frequency given for the problem means very little as you've not given the context of use (such as occurs y times over xxx hours of use).



Anyway, after re-reading you're first post I realise you must be using a child the SimpleGame or SimplePassGame, which does make matters easier, as the game loop is executed in the Main thread (as that's the thread that calls start()).



Now I realise I also got the wrong end of the stick with regard to the mouse input, it'll be a runtime choice rather then compile time (so you would not have necessarily explicitly chosen it).



To me, the likely cause is a conflict caused by the Awt/Swing and Main threads.

The Awt/Swing thread puts the events in the list and the Main thread removes them, and as the LinkedList is not synchronized there would be a problem when the Main thread tried to manage the list at the same moment in time as the Awt/Swing thread tried to manage the list (assuming non atomic operations).



Btw, the runtime implementation I use, and have had no problems with in multi threaded  use is com.jme.input.lwjgl.LWJGLMouseInput



…and my suggested solutions would be to use the LWJGLMouseInput instead of the AWTMouseInput 



Happy coding!

Thank you for help.

I firstly initializing LWJGL. When initialization fails I'll try JOGL, becouse on some kinds of old/easy graphics LWJGL is not functional but JOGL runs with older OpenGL, so I develop on more comupers for best performance.

I'll try make synchronization on AWT mouse class…