实现INewWizard的自定义文件创建向导在从Handler调用时抛出NPE

时间:2015-11-19 22:50:11

标签: eclipse-plugin eclipse-rcp

我正在扩展NewFileCreation向导,我可以通过菜单选项中的选择向导来调用它。但是当我使用处理程序从工具栏调用相同内容时,我得到了一个N​​PE。有没有办法从工具栏调用“新建文件创建向导”,因为似乎从选择向导容器字段填充向导容器,这些字段在从工具栏调用时不可用。

        Shell activeShell = HandlerUtil.getActiveShell(event);

        IWizard wizard = new CustomProjectNewScriptFile();

        WizardDialog dialog = new WizardDialog(activeShell, wizard);

        dialog.open();

        return null;

CustomProjectNewFile:

public abstract class CustomProjectNewFile extends Wizard implements INewWizard ,IExecutableExtension {

    protected WizardNewFileCreationPage _pageOne;
    private IWorkbench _workbench;
    protected IStructuredSelection _selection;

    public CustomProjectNewFile() {
        super();
    }

    public CustomProjectNewFile(String wizardName) {
        setWindowTitle(wizardName);
    }

    @Override
    public boolean performFinish() {
        boolean result = false;

        IFile file = _pageOne.createNewFile();
        result = file != null;

        if (result) {
            try {
                IDE.openEditor(_workbench.getActiveWorkbenchWindow().getActivePage(), file);
            } catch (PartInitException e) {
                e.printStackTrace();
            }
        } // else no file created...result == false

        return result;
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        _workbench = workbench;
        _selection = selection;
    }

}

CustomProjectNewScriptFile:

public class CustomProjectNewScriptFile extends CustomProjectNewFile {

    private static final String WIZARD_NAME = "New Script File"; //$NON-NLS-1$
    private IConfigurationElement _configurationElement;
    /**
     * 
     */
    public CustomProjectNewScriptFile() {
        super(WIZARD_NAME);
    }

    @Override
    public void addPages() {
        super.addPages();

        _pageOne = new WizardSchemaNewFileCreationPage(_selection);

        addPage(_pageOne);
    }

    @Override
    public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
            throws CoreException {
         _configurationElement = config;

    }
}

WizardSchemaNewFileCreationPage:

public class WizardSchemaNewFileCreationPage extends WizardNewFileCreationPage {

    private static final String PAGE_NAME = "Custom Plug-in Schema File Wizard"; //$NON-NLS-1$



    public WizardSchemaNewFileCreationPage(IStructuredSelection selection) {
        super(PAGE_NAME, selection);

        setTitle(NewWizardMessages.WizardSchemaNewFileCreationPage_Schema_File_Wizard);
        setDescription(NewWizardMessages.WizardSchemaNewFileCreationPage_Create_a_Schema_File);
        setFileExtension(NewWizardMessages.WizardSchemaNewFileCreationPage_Schema_File_Extension);
    }

    @Override
    protected InputStream getInitialContents() {
        String templateFilePath = NewWizardMessages.WizardSchemaNewFileCreationPage_Schema_Template_Location;

        StatusManager.getManager().handle(new Status(Status.INFO, Activator.PLUGIN_ID, Status.OK, templateFilePath, null), StatusManager.LOG);

        InputStream inputStream = null;
        try {

            inputStream = Activator.getDefault().getBundle().getEntry(templateFilePath).openStream();
        } catch (IOException e) {
            StatusManager.getManager().handle(new Status(Status.ERROR, Activator.PLUGIN_ID, Status.ERROR, "Failed", e), StatusManager.LOG);

        }

        return inputStream;
    }

}

例外:

org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:68)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:252)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:234)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
    at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:152)
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:493)
    at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:486)
    at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:799)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:675)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$7(HandledContributionItem.java:659)
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:592)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4180)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3769)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
Caused by: java.lang.NullPointerException
    at org.eclipse.ui.dialogs.WizardNewFileCreationPage.initialPopulateContainerNameField(WizardNewFileCreationPage.java:699)
    at org.eclipse.ui.dialogs.WizardNewFileCreationPage.createControl(WizardNewFileCreationPage.java:253)
    at org.eclipse.jface.wizard.Wizard.createPageControls(Wizard.java:175)
    at org.eclipse.jface.wizard.WizardDialog.createPageControls(WizardDialog.java:705)
    at org.eclipse.jface.wizard.WizardDialog.createContents(WizardDialog.java:597)
    at org.eclipse.jface.window.Window.create(Window.java:430)
    at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1096)
    at org.eclipse.jface.window.Window.open(Window.java:792)
    at suitacore.handlers.SuitANewScriptHandler.execute(SuitANewScriptHandler.java:23)
    at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:295)
    at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)

1 个答案:

答案 0 :(得分:0)

WizardNewFileCreationPage正在提供NPE,因为目前没有选择。

您的Wizard代码希望从init界面中声明的INewWizard方法中进行选择,但您正在使用WizardDialog来显示向导 - 调用此方法。

您可以在创建init

之前添加对WizardDialog的来电
INewWizard wizard = new CustomProjectNewScriptFile();

wizard.init(workbench, selection);

或只使用StructuredSelection.EMPTY