Tuesday 17 July 2007

Eclipse RCP: NullPointerException when creating the SaveAs dialog

When using standard editors from org.eclipse.ui.ide, the default Save As action usually fails, throwing a NullPointerException. This happens when the program tries to create a Save As Dialog. A particular exception usually looks something like:
java.lang.NullPointerException
at org.eclipse.ui.dialogs.SaveAsDialog.createContents(SaveAsDialog.java:102)
at org.eclipse.jface.window.Window.create(Window.java:426)
at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1124)
at org.eclipse.ui.texteditor.AbstractDecoratedTextEditor.performSaveAs(AbstractDecoratedTextEditor.java:1580)
at org.eclipse.ui.editors.text.TextEditor.performSaveAs(TextEditor.java:115)
at org.eclipse.ui.texteditor.AbstractTextEditor.doSaveAs(AbstractTextEditor.java:3538)
at ....MultiPageEditor.doSaveAs(MultiPageEditor.java:196)
....
This, actually, happens when the dialog tries to set the banner image for the dialog, but the image is not available. The default IDEWorkbenchAdvisor registers such an image for the default IDE, so must your application. The solution is the following:
1) Make sure you add org.eclipse.ui.ide to the dependencies of your plugin
2) In your WorkbenchAdvisor class add the following code to the initialise() method implementation:
Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH);
final String banner = "/icons/full/wizban/saveas_wiz.png";
URL url = ideBundle.getEntry(banner);
ImageDescriptor desc = ImageDescriptor.createFromURL(url);
configurer.declareImage(IDEInternalWorkbenchImages.IMG_DLGBAN_SAVEAS_DLG, desc, true);

This will declare a standard banner image from the org.eclipse.ui.ide plugin as the proper banner image for SaveAs dialog in the same way as it is done in the IDEWorkbenchAdvisor. Running your application again demonstrates the correct behaviour. If you are still getting a NullPointerException, check whether you declared plugin dependencies, and whether you included org.eclipse.ui.ide to your target platform.

7 comments:

moovida said...

That was of great, great help, thanks.
At that point I get my save as dialog, but the parent folder is missing and the OK button that should let me save is never enabled.

Is there something wrong in creating the action like this:
ActionFactory.SAVE_AS.
create(PlatformUI.getWorkbench().
getActiveWorkbenchWindow());

Andrea

Vlad said...

Oh, I'm sorry, I've been on holiday. So, I am creating these actions in my plugin's ApplicationActionBarAdvisor.
The method is called
protected void makeActions(IWorkbenchWindow window) {
// and I do it as follows:
saveAsAction = ActionFactory.SAVE_AS.create(window);
}

Anoop said...

Thanks for that info.But I am stuck with a problem in my RCP.

IDEWorkbenchPlugin & IDEInternalWorkbenchImages not resolved by Eclipse.I am using Eclipse 3.3.Any suggestions??

Thanks

Anoop

Vlad said...

Well, I am not sure if it will help, but check if you include org.eclipse.ui.ide to your dependencies.

Anoop said...

Ok I did that and the save as action work now.But the problem I face now is eclipse rcp export doesnt work.If I remove org.eclipse.ui.ide from dependencies the export works fine.
Any suggestions??

Thanks

Anoop

Vlad said...

I am using org.eclipse.ui.ide in my product and everything works fine, including exports. Try installing the complete delta pack for RCP SDK. Try updating the whole thing. If nothing helps, try reinstalling Eclipse. I do confirm, the export is working for me at the moment. I presume you synchronise your product configuration with your application plugin? Are you sure you have included org.eclipse.ui.ide to product configuration?

Michael Jastram said...

Thanks for your Info - it worked fine, but turned out not to be what I wanted to do.

Eventually, for my particular problem, I fell back on SWT, using org.eclipse.swt.widgets.FileDialog instead.