Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 331621 - Workbench throws NPE's on initial startup after install when bundles are started programmatically in earlyStartup()
Summary: Workbench throws NPE's on initial startup after install when bundles are star...
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: IDE (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.7 M5   Edit
Assignee: Paul Webster CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 356487 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-12-01 18:40 EST by rpham CLA
Modified: 2011-09-01 19:16 EDT (History)
4 users (show)

See Also:


Attachments
Error log file (2.61 KB, text/plain)
2010-12-01 18:43 EST, rpham CLA
no flags Details
safe up NPE locations v01 (2.33 KB, patch)
2011-01-05 11:08 EST, Paul Webster CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description rpham CLA 2010-12-01 18:40:11 EST
Build Identifier: M20091008-1320

Our application runs on top of the Eclipse IDE, and we're seeing NullPointerExceptions in the error log file when the it starts for the first time after installation (or with the -clean option). Besides the exceptions, there seems to be no ill effects, and the exceptions do not occur in subsequent startups. The problem seems to the related to starting bundles programmatically in our application's earlyStartup(IStartup).  

Our application is starting the following bundles in earlyStartup(): 
org.mortbay.jetty, 
org.eclipse.equinox.http.jetty, 
org.eclipse.equinox.http.registry, 
com.ibm.nex.3rdparty.spring (spring lib),
org.springframework.bundle.osgi.core,
org.springframework.bundle.osgi.web,
org.springframework.bundle.osgi.extender, 
org.springframework.bundle.osgi.io,
org.eclipse.equinox.servletbridge.

This is the exception we're getting in the Error Log:

java.lang.NullPointerException
	at org.eclipse.ui.internal.Workbench.getWorkbenchWindows(Unknown Source)
	at org.eclipse.ui.internal.ExtensionEventHandler.registryChanged(Unknown Source)
	at org.eclipse.core.internal.registry.ExtensionRegistry$2.run(Unknown Source)
	at org.eclipse.core.runtime.SafeRunner.run(Unknown Source)
	at org.eclipse.core.internal.registry.ExtensionRegistry.processChangeEvent(Unknown Source)
	at org.eclipse.core.runtime.spi.RegistryStrategy.processChangeEvent(Unknown Source)
	at org.eclipse.core.internal.registry.osgi.ExtensionEventDispatcherJob.run(Unknown Source)
	at org.eclipse.core.internal.jobs.Worker.run(Unknown Source)

It seems that windowManager is null when the getWorkbenchWindows() function gets called.

public IWorkbenchWindow[] getWorkbenchWindows() {
    Window[] windows = windowManager.getWindows();
    IWorkbenchWindow[] dwindows = new IWorkbenchWindow[windows.length];
    System.arraycopy(windows, 0, dwindows, 0, windows.length);
    return dwindows;
}




Reproducible: Always
Comment 1 rpham CLA 2010-12-01 18:43:18 EST
Created attachment 184317 [details]
Error log file
Comment 2 Dani Megert CLA 2010-12-02 03:59:02 EST
The easiest fix would be to move 
    windowManager = new WindowManager();
from init() to Workbench.Workbench(Display, WorkbenchAdvisor). This should not have any negative side-effect as the WindowManager constructor does nothing.
Comment 3 Paul Webster CLA 2010-12-02 09:22:09 EST
(In reply to comment #0)
> startups. The problem seems to the related to starting bundles programmatically
> in our application's earlyStartup(IStartup).  

While I'm not against Dani's suggestion to move up the window manager initialization, I don't believe this is the cause of your problem.

The IStartup classes are run in org.eclipse.ui.internal.Workbench.startPlugins() after init() is run (which set windowManager).

What else is going on in your product to cause this?

PW
Comment 4 rpham CLA 2010-12-02 15:35:12 EST
(In reply to comment #3)
> (In reply to comment #0)
> > startups. The problem seems to the related to starting bundles programmatically
> > in our application's earlyStartup(IStartup).  
> 
> While I'm not against Dani's suggestion to move up the window manager
> initialization, I don't believe this is the cause of your problem.
> 
> The IStartup classes are run in
> org.eclipse.ui.internal.Workbench.startPlugins() after init() is run (which set
> windowManager).
> 
> What else is going on in your product to cause this?
> 
> PW

(In reply to comment #3)
> (In reply to comment #0)
> > startups. The problem seems to the related to starting bundles programmatically
> > in our application's earlyStartup(IStartup).  
> 
> While I'm not against Dani's suggestion to move up the window manager
> initialization, I don't believe this is the cause of your problem.
> 
> The IStartup classes are run in
> org.eclipse.ui.internal.Workbench.startPlugins() after init() is run (which set
> windowManager).
> 
> What else is going on in your product to cause this?
> 
> PW

You're right, it's not related to code in IStartup. Sorry, it's been awhile since I looked at the code. In our UIPlugin bundle's start() function, we're also programmatically installing and starting two other bundles (this probably triggered the call to getWorkbenchWindows() prematurely); this was our workaround for not being able to figure out why OSGi is not recognizing these two bundles. The exception went away when this call is commented out.

Thanks,
Ryan
Comment 5 Dani Megert CLA 2010-12-03 01:52:33 EST
(In reply to comment #4)
>The exception went away when this call is commented out.
OK to close then?
Comment 6 rpham CLA 2010-12-03 13:29:43 EST
(In reply to comment #5)
> (In reply to comment #4)
> >The exception went away when this call is commented out.
> OK to close then?

Unfortunately, we still need this call; it was commented out to help identify the cause. This does provide us with a solution - to delay this call by calling it in the earlyStartup(); however, this is not favorable since it would increase wait time (after the IDE is up) for the availability of the web services that are provided by the programmatically installed bundles.
Comment 7 Paul Webster CLA 2010-12-06 09:13:02 EST
(In reply to comment #6)
> Unfortunately, we still need this call; it was commented out to help identify
> the cause. This does provide us with a solution - to delay this call by calling
> it in the earlyStartup(); however, this is not favorable since it would
> increase wait time (after the IDE is up) for the availability of the web
> services that are provided by the programmatically installed bundles.

I'm not convinced moving this call up is a good idea.  While the window manager construction is harmless, the gist of the problem is the Workbench is being accessed before the workbench has been fully initialized.  Removing the NPE could simply allow the code to continue, doing things incorrectly.


It's possible to replace the offending code in ExtensionEventHandler with:

Display display = PlatformUI.getWorkbench().getDisplay();

But once again, with the workbench not fully initialized, any changes that are to be processed by this will be thrown away, or half processed.

PW
Comment 8 Dani Megert CLA 2010-12-06 09:18:22 EST
>I'm not convinced moving this call up is a good idea. 
From an API/client perspective throwing an NPE is not expected hence fixing this seems right, even though it does not guaranteed that the client works afterwards. The client should use the Workbench.is* methods to check the state.
Comment 9 Paul Webster CLA 2011-01-05 11:08:21 EST
Created attachment 186093 [details]
safe up NPE locations v01

I safed up getWorkbenchWindows() and getWorkbenchWindowCount() so they won't NPE, and simply got the Display from PlatformUI.getWorkbench().getDisplay() in ExtensionEventHandler.

rpham, could you test this solves your problem?

PW
Comment 10 Paul Webster CLA 2011-01-05 12:43:51 EST
Released to HEAD
PW
Comment 11 Paul Webster CLA 2011-01-25 09:00:28 EST
I the installed plugins in I20110124-1800
PW
Comment 12 Remy Suen CLA 2011-09-01 19:16:02 EDT
*** Bug 356487 has been marked as a duplicate of this bug. ***