EMenuService在@postConstruct中抛出nullpointerException

时间:2014-11-10 10:11:52

标签: java eclipse-rcp e4

我正在尝试在我的RCP E4浏览器中自定义鼠标右键,我在application.e4xmi中创建了弹出菜单,这是我触发右键单击事件的代码。

用于在零件堆栈内创建零件:

 public void showBrowser(Composite parent, EModelService service,
            MApplication application, EPartService partservice) {
        MPartStack mpartstack = (MPartStack) service.find(
                "com.example.e4.rcp.wizard.partstack.confps", application);
        MPart navigationpart = partservice
                .createPart("com.example.e4.rcp.wizard.partdescriptor.navigate_pd");
        navigationpart.setLabel("Visual Navigator");
        // for adding the menucontext

        //
        mpartstack.getChildren().add(navigationpart);
        MPartStack metadata_ps = (MPartStack) service.find(
                "com.example.e4.rcp.wizard.partstack.metadataPS", application);
        metadata_ps.setVisible(true);
        MPart metadatapart = partservice
                .createPart("com.example.e4.rcp.wizard.partdescriptor.metadatadesc");
        metadatapart.setLabel("Metadata");
        metadata_ps.getChildren().add(metadatapart);
    }

在部分描述符中:“com.example.e4.rcp.wizard.partdescriptor.navigate_pd”

@Inject
    IEclipseContext context;
    EMenuService menuservice;

    @PostConstruct
    public void createControll(Composite parent, EModelService service,
            MApplication application) {
   // some other stuffs
   menuservice.registerContextMenu(browser, "com.example.e4.rcp.wizard.popupmenu");
}

它在运行时显示NullPointerException,我也尝试在postconstruct方法中注入EMenuService,但在这种情况下,预期结果会更糟(你知道为什么)。我是RCP E4中的新手,如果我犯了任何基本错误,那就高兴了。

1 个答案:

答案 0 :(得分:0)

您必须在要注入的每个字段上使用@Inject:

@Inject
IEclipseContext context;
@Inject
EMenuService menuservice;

您现有的代码只要求注入IEclipseContext