Reputation:
//obtain the active page
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
returns Exception in thread "Thread-3" java.lang.NullPointerExceptionµ. What shall i do?
Upvotes: 10
Views: 9268
Reputation: 37
I have a work-around for this. even though its an old post.
IWorkbench wb = PlatformUI.getWorkbench();
if (wb.getWorkbenchWindowCount() == 1) {
try{
wb.getWorkbenchWindows()[0].getActivePage().getPerspective();
}
catch(NullPointerException e)
{
Logger.log(e);
}
}
Upvotes: 1
Reputation: 391
If the thread does not run in the active window, PlatformUI.getWorkbench().getActiveWorkbenchWindow() will return "null". You must wrap your code in a Display, e.g.:
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
});
Upvotes: 39
Reputation: 84068
Add some null checks, it is possible for the workbench to not have an active window, not it is also possible for PlatformUI.getWorkbench to throw an IllegalStateException if the workbench is not yet started (e.g createAndRunWorkbench() has not yet been called).
IWorkbenchWindow window = PlatformUI.getWorkbench().getInstance()
.getActiveWorkbenchWindow();
if(workbenchWindow != null) {
IWorkbenchPage page = window .getActivePage();
}
Upvotes: 3