AnActionEvent.getData(PlatformDataKeys.VIRTUAL_FILE) result is null

hello :

I guess it is a bug:


@Override
public void update(@NotNull AnActionEvent e) {
    VirtualFile selectedFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
}

selectedFile IS NULL .

my versions:
IntelliJ IDEA 2025.2.3 (Ultimate Edition)
id ‘org.jetbrains.intellij.platform’ version ‘2.10.2’

my code:

build.gradle

plugins {
    id 'java'
//https://github.com/JetBrains/intellij-platform-gradle-plugin/releases
    id 'org.jetbrains.intellij.platform' version '2.10.2'
    // 可选: 迁移插件以检查配置
    id 'org.jetbrains.intellij.platform.migration' version '2.10.2'
}

group = "com.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    intellijPlatform {
        defaultRepositories()
    }
}

dependencies {
    intellijPlatform {
        create("IU", "2025.2.3") {
            useInstaller = false  // 使用multi-OS ZIP档案,避免installer解析问题
        }
        jetbrainsRuntime()  // 添加JBR依赖,确保运行IDE任务
    }
}

intellijPlatform {
    pluginConfiguration {
        ideaVersion {
            sinceBuild = "252"
        }
    }
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
    options.compilerArgs += ['-Xlint:unchecked', '-Xlint:deprecation', '-parameters']
}

action:


<actions>
    <action id="AutoGenSignal" class="com.example.signal.AutoGenSignal" text="AutoGenSignal" description="AutoGenSignal">
        <add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
    </action>
</actions>

custom code:


public class AutoGenSignal extends AnAction {

    // 控制菜单是否显示:仅对*.job文件可见
    @Override
    public void update(@NotNull AnActionEvent e) {
        VirtualFile selectedFile = e.getData(CommonDataKeys.VIRTUAL_FILE);
        System.out.println(selectedFile); // ERROR!!!!   selectedFile is null

        String jobPath = selectedFile.getPath();
        System.out.println(jobPath);
        System.out.println(selectedFile.isDirectory());


//        e.getPresentation().setVisible(isVisible);
    }

    @Override
    public void actionPerformed(AnActionEvent e) {
    }

}

Hello!

There is no documented guarantee that the VIRTUAL_FILE is not null. Broadly, this can happen when there is no selection.

You have to address this by testing explicitly for != null.

thank you. I understand what you mean.

But my plugin needs to right-click on a certain file to get the path of the currently clicked file. Now the problem is that I can’t get the selected file, not by skipping the check with !=null.

This way of writing is acceptable.

Object[] selectedItems = e.getData(PlatformCoreDataKeys.SELECTED_ITEMS);