AnActionEvent "cannot share data context between Swing events"
Answered
Hello,
I am currently having an issue of "cannot share data context between swing events" that is pretty random and not easily reproduced but does occur every so often. I believe I am following the appropriate practice of saving off the data and then using it but maybe I'm not? I've posted the error and the code below and the entire file is located on github:
Any help would be greatly appreciated!
ERROR:
cannot share data context between Swing events; initial event count = 619535; current event count = 619729
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:127)
at com.intellij.ide.impl.DataManagerImpl$MyDataContext.getData(DataManagerImpl.java:361)
at com.intellij.openapi.actionSystem.DataKey.getData(DataKey.java:75)
at com.intellij.openapi.actionSystem.AnActionEvent.getData(AnActionEvent.java:165)
at actions.UploadAction.actionPerformed(UploadAction.java:55)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$3.performAction(IdeKeyEventDispatcher.java:595)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(IdeKeyEventDispatcher.java:646)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.b(IdeKeyEventDispatcher.java:485)
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(IdeKeyEventDispatcher.java:215)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:631)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:386)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
CODE:
Note that I read to use final when grabbing anything off of action event.
final VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
Please sign in to leave a comment.
Chris, DataContext is an abstraction of current focus position. You can't use DataContext if focus has changed (say a popup was shown, editor is closed or not focused anymore, etc). We guarantee that focus and components hierarchy won't be changed in actionPerfomed() method until you explicitly request it (by showing modal dialogs, or passing data context into another thread, or passing it into invokeLater(), and so on).
General approach here is to collect necessary data before you trigger any UI activity.
Please try putting
I will try that. Thank you. Out of curiosity, do you know why would this make a difference in this particular context? There's not much going on in the method.
Thank you Konstantin, that makes sense. I will use this approach going forward.