Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Page NullPointerException when trying to register a listener
Page NullPointerException when trying to register a listener [message #6986] Mon, 16 June 2008 08:21 Go to next message
Eclipse UserFriend
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 #7008 is a reply to message #6986] Mon, 16 June 2008 13:59 Go to previous messageGo to next message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
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 #7031 is a reply to message #6986] Mon, 16 June 2008 14:00 Go to previous message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
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 13:59 Go to previous message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
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 14:00 Go to previous message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
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.
Previous Topic:Page NullPointerException when trying to register a listener
Next Topic:Picasso problem
Goto Forum:
  


Current Time: Fri Apr 26 05:39:53 GMT 2024

Powered by FUDForum. Page generated in 0.03730 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top