NullPointerException in PlatformUI.getWorkbench (). GetActiveWorkbenchWindow (). GetActivePage ()

//obtain the active page IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 

returns an exception in the thread-3 thread java.lang.NullPointerExceptionμ. What should I do?

+8
eclipse-plugin
source share
3 answers

If the thread does not start in the active window, PlatformUI.getWorkbench (). getActiveWorkbenchWindow () will return "null". You must wrap your code on the display, for example:

 Display.getDefault().asyncExec(new Runnable() { @Override public void run() { IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); } }); 
+36
source share

Add a few null checks, it is possible that the workstation does not have an active window, but it is also impossible for PlatformUI.getWorkbench to throw an IllegalStateException if the desktop is not already running (for example, createAndRunWorkbench () has not yet been called).

 IWorkbenchWindow window = PlatformUI.getWorkbench().getInstance() .getActiveWorkbenchWindow(); if(workbenchWindow != null) { IWorkbenchPage page = window .getActivePage(); } 
+2
source share

I have a workaround for this. although this is an old post.

  IWorkbench wb = PlatformUI.getWorkbench(); if (wb.getWorkbenchWindowCount() == 1) { try{ wb.getWorkbenchWindows()[0].getActivePage().getPerspective(); } catch(NullPointerException e) { Logger.log(e); } } 
0
source share

All Articles