AnActionEvent "cannot share data context between Swing events"

   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!
cannot share data context between Swing events; initial event count = 619535; current event count = 619729
at com.intellij.openapi.diagnostic.Logger.error(
at com.intellij.ide.impl.DataManagerImpl$MyDataContext.getData(
at com.intellij.openapi.actionSystem.DataKey.getData(
at com.intellij.openapi.actionSystem.AnActionEvent.getData(
at actions.UploadAction.actionPerformed(
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher$3.performAction(
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.processAction(
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.b(
at com.intellij.openapi.keymap.impl.IdeKeyEventDispatcher.dispatchKeyEvent(
at com.intellij.ide.IdeEventQueue._dispatchEvent(
at com.intellij.ide.IdeEventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(
Note that I read to use final when grabbing anything off of action event.
final VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
Comment actions Permalink
Official 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.

Comment actions Permalink

Please try putting

final VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
as first statement in actionPerformed() method.
Comment actions Permalink

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.

Comment actions Permalink

Thank you Konstantin, that makes sense. I will use this approach going forward.


Please sign in to leave a comment.