Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 328977

Summary: NewProjectCreationOperation: NPE when executing in non UI thread
Product: [Eclipse Project] PDE Reporter: Gerhard Leonhartsberger <gerhard_leonhartsberger>
Component: UIAssignee: Curtis Windatt <curtis.windatt.public>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: curtis.windatt.public
Version: 3.7   
Target Milestone: 3.7 M7   
Hardware: All   
OS: All   
Whiteboard:

Description Gerhard Leonhartsberger CLA 2010-10-28 16:22:53 EDT
Build Identifier: M20100909-0800

NewProjectCreationOperation cannot be executed in a non UI thread because openFile(IFile) calls PDEPlugin.getActiveWorkbenchWindow() which returns null when invoked from a non UI thread. Please see the current code fragment of NewProjectCreationOperation that comes with Eclipse 3.6.1:

private void openFile(final IFile file) {
  final IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
  final IWorkbenchPage page = ww.getActivePage();
  if (page == null)
	return;
  final IWorkbenchPart focusPart = page.getActivePart();
  ww.getShell().getDisplay().asyncExec(new Runnable() {
    public void run() {
      if (focusPart instanceof ISetSelectionTarget) {
        ISelection selection = new StructuredSelectionfile);
        ((ISetSelectionTarget) focusPart).selectReveal(selection);
      }
      try {
        IDE.openEditor(page, file, true);
      } catch (PartInitException e) {
      }
    }
  });
}

The implementation of openFile(IFile) should be changed as shown below:

private void openFile(final IFile file) {

  PDEPlugin.getDefault().getWorkbench().getDisplay().asyncExec(new Runnable() {
    public void run() {
      final IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
      final IWorkbenchPage page = ww.getActivePage();
      if (page == null)
	return;
      final IWorkbenchPart focusPart = page.getActivePart();
      if (focusPart instanceof ISetSelectionTarget) {
        ISelection selection = new StructuredSelectionfile);
        ((ISetSelectionTarget) focusPart).selectReveal(selection);
      }
      try {
        IDE.openEditor(page, file, true);
      } catch (PartInitException e) {
      }
    }
  });
}


Reproducible: Always

Steps to Reproduce:
as mentioned above
Comment 1 Curtis Windatt CLA 2010-10-29 10:48:31 EDT
Note that NewProjectCreationOperation is an internal UI class.  There has been interest in making it api, but the work never materialized.  How the job works may change between releases.  Is this NPE the only issue preventing you from using the job in a non-UI thread?
Comment 2 Gerhard Leonhartsberger CLA 2010-10-29 11:02:46 EDT
Yes, it's the only issue. Only care must be taken when implementing IProjectProvider. The IProjectProvider's methods must also run in UI thread. It would be nice if NewProjectCreationOperation could consider this.
Comment 3 Curtis Windatt CLA 2011-03-14 17:03:08 EDT
Fixed in HEAD.  See PDEPlugin and NewProjectCreationOperation.
Comment 4 Curtis Windatt CLA 2011-03-14 17:03:22 EDT
Fixed.