questseekr
questseekr

Reputation: 83

NullpointerException in IDEWorkbenchAdvisor during Workbench.close() in earlyStartup() of own plugin

I am currently writing an own Eclipse plugin that should run in the background and close itself if a file appears in the system.

I used the org.eclipse.ui.startup extension and I am waiting until workbench.isStarting() is false. But when I call workbench.close() I get a NullPointerException

Current Code:

public class Startup1 implements IStartup {

    @Override
    public void earlyStartup() {


        File file = new File("c:\\closeworkbench");

        while(!file.exists()) {
            try {
                Thread.sleep(2000);

            } catch (InterruptedException e) {

            }
        }


        IWorkbench workbench = PlatformUI.getWorkbench();
        while(workbench.isStarting())
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }
        workbench.close();

    }

}

And here is the NullpointerException.

!SESSION 2019-05-09 10:13:29.634 -----------------------------------------------
eclipse.buildId=4.11.0.I20190307-0500
java.version=1.8.0_181
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=de_DE
Framework arguments:  -product org.eclipse.platform.ide
Command-line arguments:  -product org.eclipse.platform.ide -data C:\plugindevworkspace/../runtime-EclipseApplication -dev file:C:/plugindevworkspace/.metadata/.plugins/org.eclipse.pde.core/Eclipse Application/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog

!ENTRY org.eclipse.egit.core 1 0 2019-05-09 10:13:33.541
!MESSAGE Using Apache MINA sshd as ssh client.
log4j:WARN No appenders could be found for logger (org.eclipse.buildship.core.internal.util.gradle.PublishedGradleVersions).
log4j:WARN Please initialize the log4j system properly.

!ENTRY org.eclipse.egit.ui 2 0 2019-05-09 10:13:38.118
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'Y:\'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
It works
Closing workbench

!ENTRY org.eclipse.ui.workbench 4 2 2019-05-09 10:13:39.238
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.ui.workbench".
!STACK 0
java.lang.NullPointerException
    at org.eclipse.ui.internal.ide.application.IDEWorkbenchAdvisor.preShutdown(IDEWorkbenchAdvisor.java:369)
    at org.eclipse.ui.internal.Workbench.busyClose(Workbench.java:1078)
    at org.eclipse.ui.internal.Workbench.lambda$4(Workbench.java:1413)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:55)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1413)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1386)
    at de.pds.autoshutdown.Startup1.earlyStartup(Startup1.java:50)
    at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:80)
    at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:56)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
    at org.eclipse.ui.internal.Workbench$39.run(Workbench.java:2707)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

!ENTRY org.eclipse.ui 4 0 2019-05-09 10:13:39.240
!MESSAGE Unable to execute early startup code for the org.eclipse.ui.IStartup extension contributed by the 'de.pds.autoshutdown' plug-in.
!STACK 0
java.lang.NullPointerException
    at org.eclipse.ui.internal.ide.application.IDEWorkbenchAdvisor.preShutdown(IDEWorkbenchAdvisor.java:369)
    at org.eclipse.ui.internal.Workbench.busyClose(Workbench.java:1078)
    at org.eclipse.ui.internal.Workbench.lambda$4(Workbench.java:1413)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:55)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1413)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1386)
    at de.pds.autoshutdown.Startup1.earlyStartup(Startup1.java:50)
    at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:80)
    at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:56)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
    at org.eclipse.ui.internal.Workbench$39.run(Workbench.java:2707)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

Do you have any ideas?

Upvotes: 0

Views: 355

Answers (1)

greg-449
greg-449

Reputation: 111216

The earlyStartup method is not running in the UI thread. But the IDEWorkbenchAdvisor.preShutdown method called by IWorkbench.close expects to run in the UI thread since it calls Display.getCurrent which only works in the UI thread.

You could try using Display.asyncExec to run the close in the UI thread as soon as possible:

Display.getDefault().asyncExec(() -> workbench.close());

However I am not sure if things like IWorkbench.isStarted are thread safe so what you are trying to do may not be reliable.

Upvotes: 1

Related Questions