0
votes

I'm using this code I found on the internet to open the WST wizard that helps creating a new XML file.

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();
    }
   }

I also defined the id of the wizard :

String XML_WIZARD_ID= "org.eclipse.wst.xml.ui.internal.wizards.NewXMLWizard";

and I call this function with :

openWizard(XML_WIZARD_ID);

This does't work and throws a java.lang.NullPointerException in the internal code of Eclipse.

I would like to know what can cause the problem.

Here is the stack trace :

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)

1
Can you show the stack trace for the exception? The one that presumably gets printed out? - nitind
Edited with the stack trace. - AAmine

1 Answers

1
votes

The code you found on the internet is incomplete. New, Import and Export wizards implement IWorkbenchWizard and require that the init method is called before showing the wizard:

public void init(IWorkbench workbench, IStructuredSelection selection)

The NullPointerException is because the selection has not been set.

So after creating the wizard and before passing to WizardDialog you need to do something like:

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

where selection is the current selection or StructuredSelection.EMPTY, but not null.