gpt4 book ai didi

java - Eclipse WST 打开新的 XML 向导抛出 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:16:40 25 4
gpt4 key购买 nike

我正在使用在互联网上找到的这段代码来打开 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) at org.eclipse.ui.dialogs.WizardNewFileCreationPage.createControl(WizardNewFileCreationPage.java:247) at org.eclipse.wst.xml.ui.internal.wizards.NewModelWizard$NewFilePage.createControl(NewModelWizard.java:283) at org.eclipse.jface.wizard.Wizard.createPageControls(Wizard.java:174) at org.eclipse.jface.wizard.WizardDialog.createPageControls(WizardDialog.java:736) at org.eclipse.jface.wizard.WizardDialog.createContents(WizardDialog.java:608) at 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) at org.eclipse.linuxtools.tmf.ui.views.xmlExplorer.XMLExplorerView$AddAction.run(XMLExplorerView.java:140) at org.eclipse.jface.action.Action.runWithEvent(Action.java:499) at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584) at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501) at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:452) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1392) at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3742) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3363) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997) at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138) at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:610) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567) at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150) at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354) at 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636) at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591) at org.eclipse.equinox.launcher.Main.run(Main.java:1450) at org.eclipse.equinox.launcher.Main.main(Main.java:1426)

最佳答案

您在互联网上找到的代码不完整。新的导入和导出向导实现了 IWorkbenchWizard 并要求在显示向导之前调用 init 方法:

public void init(IWorkbench workbench, IStructuredSelection selection)

NullPointerException 是因为尚未设置选择。

因此,在创建向导之后并在传递到 WizardDialog 之前,您需要执行以下操作:

if (wizard instanceof IWorkbenchWizard) {
((IWorkbenchWizard)wizard).init(workbench, selection);
}

其中selection是当前选择或StructuredSelection.EMPTY,但不为空。

关于java - Eclipse WST 打开新的 XML 向导抛出 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22414704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com