Page NullPointerException when trying to register a listener [message #6986] |
Mon, 16 June 2008 04:21  |
Eclipse User |
|
|
|
Originally posted by: franzi483.web.de
Hello!
I am writing a plug-in for Eclipse, and I want to register a
PartListener which implements IPartListener2 when a particular view of
my plug-in is created.
I try to achieve this by the following code:
createPartControl(Composite c) {
...
IWorkbenchWindow workbenchWindow;
IWorkbenchPage page;
IWorkbench wbench = PlatformUI.getWorkbench();
workbenchWindow = wbench.getActiveWorkbenchWindow();
page = workbenchWindow.getActivePage();
if (partListener == null) {
partListener = new PartListener();
page.addPartListener(partListener);
}
...
}
But it always returns me a NullPointerException in the line
page.addPartListener as page is null.
I've tried the same code in the plugin initialization (in method
start(BundleContext b)). There it randomly shows a NullPointerException.
Any idea why the page is null?
Thank you in advance for any advice.
|
|
|
|
Re: Page NullPointerException when trying to register a listener [message #7031 is a reply to message #6986] |
Mon, 16 June 2008 10:00  |
Eclipse User |
|
|
|
FWIW, this sort of question probably belongs in eclipse.platform.
Wayne
On Mon, 2008-06-16 at 10:21 +0200, Franz Brenner wrote:
> Hello!
>
> I am writing a plug-in for Eclipse, and I want to register a
> PartListener which implements IPartListener2 when a particular view of
> my plug-in is created.
>
> I try to achieve this by the following code:
>
> createPartControl(Composite c) {
> ...
> IWorkbenchWindow workbenchWindow;
> IWorkbenchPage page;
> IWorkbench wbench = PlatformUI.getWorkbench();
> workbenchWindow = wbench.getActiveWorkbenchWindow();
> page = workbenchWindow.getActivePage();
> if (partListener == null) {
> partListener = new PartListener();
> page.addPartListener(partListener);
> }
> ...
> }
>
> But it always returns me a NullPointerException in the line
> page.addPartListener as page is null.
>
> I've tried the same code in the plugin initialization (in method
> start(BundleContext b)). There it randomly shows a NullPointerException.
>
> Any idea why the page is null?
>
> Thank you in advance for any advice.
|
|
|
Re: Page NullPointerException when trying to register a listener [message #569063 is a reply to message #6986] |
Mon, 16 June 2008 09:59  |
Eclipse User |
|
|
|
With the activator, you're probably running into a thread issue. Only
the UI thread can call getActiveWorkbenchWindow() (checkout the comments
in Workbench#getActiveWorkbenchWindow()).
Try instead something like:
createPartControl(Composite c) {
page = getSite().getPage();
...
}
Not sure if it will help, but it will make the code tighter (and more
correct).
Wayne
On Mon, 2008-06-16 at 10:21 +0200, Franz Brenner wrote:
> Hello!
>
> I am writing a plug-in for Eclipse, and I want to register a
> PartListener which implements IPartListener2 when a particular view of
> my plug-in is created.
>
> I try to achieve this by the following code:
>
> createPartControl(Composite c) {
> ...
> IWorkbenchWindow workbenchWindow;
> IWorkbenchPage page;
> IWorkbench wbench = PlatformUI.getWorkbench();
> workbenchWindow = wbench.getActiveWorkbenchWindow();
> page = workbenchWindow.getActivePage();
> if (partListener == null) {
> partListener = new PartListener();
> page.addPartListener(partListener);
> }
> ...
> }
>
> But it always returns me a NullPointerException in the line
> page.addPartListener as page is null.
>
> I've tried the same code in the plugin initialization (in method
> start(BundleContext b)). There it randomly shows a NullPointerException.
>
> Any idea why the page is null?
>
> Thank you in advance for any advice.
|
|
|
Re: Page NullPointerException when trying to register a listener [message #569099 is a reply to message #6986] |
Mon, 16 June 2008 10:00  |
Eclipse User |
|
|
|
FWIW, this sort of question probably belongs in eclipse.platform.
Wayne
On Mon, 2008-06-16 at 10:21 +0200, Franz Brenner wrote:
> Hello!
>
> I am writing a plug-in for Eclipse, and I want to register a
> PartListener which implements IPartListener2 when a particular view of
> my plug-in is created.
>
> I try to achieve this by the following code:
>
> createPartControl(Composite c) {
> ...
> IWorkbenchWindow workbenchWindow;
> IWorkbenchPage page;
> IWorkbench wbench = PlatformUI.getWorkbench();
> workbenchWindow = wbench.getActiveWorkbenchWindow();
> page = workbenchWindow.getActivePage();
> if (partListener == null) {
> partListener = new PartListener();
> page.addPartListener(partListener);
> }
> ...
> }
>
> But it always returns me a NullPointerException in the line
> page.addPartListener as page is null.
>
> I've tried the same code in the plugin initialization (in method
> start(BundleContext b)). There it randomly shows a NullPointerException.
>
> Any idea why the page is null?
>
> Thank you in advance for any advice.
|
|
|
Powered by
FUDForum. Page generated in 0.08071 seconds