我使用我在互联网上找到的代码来打开WST向导,该向导有助于创建新的XML文件。
public void openWizard(String id) { // First see if this is a "new wizard". IWizardDescriptor descriptor = PlatformUI.getWorkbench() .getNewWizardRegistry().findWizard(id); // If not check if it is an "import wizard". if (descriptor == null) { descriptor = PlatformUI.getWorkbench().getImportWizardRegistry() .findWizard(id); } // Or maybe an export wizard if (descriptor == null) { descriptor = PlatformUI.getWorkbench().getExportWizardRegistry() .findWizard(id); } try {
// Then if we have a wizard, open it.
if (descriptor != null) {
IWizard wizard = descriptor.createWizard();
IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
WizardDialog wd = new WizardDialog(win.getShell(), wizard);
wd.setTitle(wizard.getWindowTitle());
wd.open();
}
} catch (CoreException e) {
e.printStackTrace();
}
}
我还定义了向导的id:
String XML_WIZARD_ID= "org.eclipse.wst.xml.ui.internal.wizards.NewXMLWizard";
我将此函数称为:
openWizard(XML_WIZARD_ID);
这不起作用,并在Eclipse的内部代码中抛出java.lang.NullPointerException。
我想知道可能导致问题的原因。
这是堆栈跟踪:
java.lang.NullPointerException at org.eclipse.ui.dialogs.WizardNewFileCreationPage.initialPopulateContainerNameField(WizardNewFileCreationPage.java:688) 在 org.eclipse.ui.dialogs.WizardNewFileCreationPage.createControl(WizardNewFileCreationPage.java:247) 在 org.eclipse.wst.xml.ui.internal.wizards.NewModelWizard $ NewFilePage.createControl(NewModelWizard.java:283) 在 org.eclipse.jface.wizard.Wizard.createPageControls(Wizard.java:174) 在 org.eclipse.jface.wizard.WizardDialog.createPageControls(WizardDialog.java:736) 在 org.eclipse.jface.wizard.WizardDialog.createContents(WizardDialog.java:608) 在org.eclipse.jface.window.Window.create(Window.java:432)at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1104)at org.eclipse.jface.window.Window.open(Window.java:791)at org.eclipse.linuxtools.tmf.ui.views.xmlExplorer.XMLExplorerView.openWizard(XMLExplorerView.java:286) 在 org.eclipse.linuxtools.tmf.ui.views.xmlExplorer.XMLExplorerView $ AddAction.run(XMLExplorerView.java:140) 在org.eclipse.jface.action.Action.runWithEvent(Action.java:499)at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584) 在 org.eclipse.jface.action.ActionContributionItem.access $ 2(ActionContributionItem.java:501) 在 org.eclipse.jface.action.ActionContributionItem $ 6.handleEvent(ActionContributionItem.java:452) 在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1392)at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3742) 在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3363) 在 org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 9.run(PartRenderingEngine.java:1113) 在 org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 在 org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997) 在 org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138) 在org.eclipse.ui.internal.Workbench $ 5.run(Workbench.java:610)at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 在 org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567) 在 org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150) 在 org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) 在 org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) 在 org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 在 org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354) 在 org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)at at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)at at org.eclipse.equinox.launcher.Main.run(Main.java:1450)at org.eclipse.equinox.launcher.Main.main(Main.java:1426)
答案 0 :(得分:1)
您在互联网上找到的代码不完整。新建,导入和导出向导实现IWorkbenchWizard
并要求在显示向导之前调用init
方法:
public void init(IWorkbench workbench, IStructuredSelection selection)
NullPointerException
是因为尚未设置选择。
因此,在创建向导之后,在传递给WizardDialog
之前,您需要执行以下操作:
if (wizard instanceof IWorkbenchWizard) {
((IWorkbenchWizard)wizard).init(workbench, selection);
}
其中selection
是当前选择或StructuredSelection.EMPTY
,但不是空。