ConcurrentModificationException in hosted mode while operating with listeners

6 views
Skip to first unread message

Val

unread,
Mar 21, 2007, 5:12:39 AM3/21/07
to Google Web Toolkit
Hi! Take a look at this simple code:

private final KeyboardListener keyboardListener
= new KeyboardListenerAdapter()
{
public void onKeyDown(Widget widget,char c, int i)
{
super.onKeyDown(widget, c, i);
if (KeyboardListener.KEY_ENTER == c)
{
textBox.removeKeyboardListener(keyboardListener);
}
}
};

public void onModuleLoad()
{
textBox.addKeyboardListener(keyboardListener);
}

I'm getting the following exception in hosted mode:


[ERROR] Uncaught exception escaped
java.util.ConcurrentModificationException: null
at java.util.AbstractList
$Itr.checkForComodification(AbstractList.java:448)
at java.util.AbstractList$Itr.next(AbstractList.java:419)
at
com.google.gwt.user.client.ui.KeyboardListenerCollection.fireKeyDown(KeyboardListenerCollection.java:
81)
at
com.google.gwt.user.client.ui.KeyboardListenerCollection.fireKeyboardEvent(KeyboardListenerCollection.java:
55)
at
com.google.gwt.user.client.ui.TextBoxBase.onBrowserEvent(TextBoxBase.java:
167)
at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:968)
at com.google.gwt.user.client.DOM.dispatchEventAndCatch(DOM.java:951)
at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:919)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:324)

Should it bother me since i'm not getting any errors in javascript and
browsers are single-threaded? Well actually it annoys me cause i don't
like any errors showing=)

Sandy McArthur

unread,
Mar 21, 2007, 9:49:16 AM3/21/07
to Google-We...@googlegroups.com
The easiest solution would be to use a DeferredCommand to remove the
listener. This will execute after the current list of listeners have
been processed avoiding the ConcurrentModificationException. That said
you may get more keyboard events that you should ignore between when
you schedule the DeferredCommand and it actually runs, so be careful.

DeferredCommand.add(new Command() {
public void execute() {
textBox.removeKeyboardListener(keyboardListener);
}
});


--
Sandy McArthur

"He who dares not offend cannot be honest."
- Thomas Paine

Reply all
Reply to author
Forward
0 new messages