Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » getActivePage throws NullPointerException
getActivePage throws NullPointerException [message #327643] Mon, 28 April 2008 02:10 Go to next message
Eclipse UserFriend
Hi,

I have a view and in the constructor of my view, I have this line

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActivePart()...
(ignore the remaining :D)

Now, when the view is open when I lunch eclipse, it throws
NullPointerException because getActivePage() returns null.

It happens randomly. Sometimes, it's null sometimes not. How should I deal
it? Please help.


Cheers,
Allan
Re: getActivePage throws NullPointerException [message #327647 is a reply to message #327643] Mon, 28 April 2008 03:28 Go to previous messageGo to next message
Eclipse UserFriend
I would implement the IPartListener2 interface and would monitor easily
the active part.

Why do you need the active part when creating the view?
If view1 opens view2, it is easy to view1 to get a reference to view2, and
then pass back a reference to this to view2.


class View1 {
void openChildView() {
View2 view2 =
(View2)getSite().getWorkbenchWindow().getActivePage().showVi ew( "view2.id");
view2.setParent(this);
}
}

class View2 {
void setParent(View1 view1) {
....
}
}

Allan Ramirez wrote:

> Hi,

> I have a view and in the constructor of my view, I have this line

>
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActivePart()...
> (ignore the remaining :D)

> Now, when the view is open when I lunch eclipse, it throws
> NullPointerException because getActivePage() returns null.

> It happens randomly. Sometimes, it's null sometimes not. How should I deal
> it? Please help.
Re: getActivePage throws NullPointerException [message #327652 is a reply to message #327647] Mon, 28 April 2008 04:08 Go to previous messageGo to next message
Eclipse UserFriend
It's not the activePart that I'm really after. I want to add a selection
listener to the active page so that when the user will click on anything,
I would do something to my view.


Here's what my line of my code in the constructor looks like..

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActivePart().getSite().getSelectionProvider().ad dSelectionChangedListener(
getSelectionChangedListener() );

Instead of the above, I could also replace it with the one below.
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().addSelectionListener(getSelectionListener());


However, getActivePage would sometimes return null.


Any better approach or codes other than the above lines is highly
appreciated.



David Perez wrote:

> I would implement the IPartListener2 interface and would monitor easily
> the active part.

> Why do you need the active part when creating the view?
> If view1 opens view2, it is easy to view1 to get a reference to view2, and
> then pass back a reference to this to view2.


> class View1 {
> void openChildView() {
> View2 view2 =
> (View2)getSite().getWorkbenchWindow().getActivePage().showVi ew( "view2.id");
> view2.setParent(this);
> }
> }

> class View2 {
> void setParent(View1 view1) {
> ....
> }
> }

> Allan Ramirez wrote:

>> Hi,

>> I have a view and in the constructor of my view, I have this line

>>
>
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActivePart()...
>> (ignore the remaining :D)

>> Now, when the view is open when I lunch eclipse, it throws
>> NullPointerException because getActivePage() returns null.

>> It happens randomly. Sometimes, it's null sometimes not. How should I deal
>> it? Please help.
Re: getActivePage throws NullPointerException [message #327660 is a reply to message #327643] Mon, 28 April 2008 09:17 Go to previous messageGo to next message
Eclipse UserFriend
I'll bet that getActiveWorkbenchWindow() is what's really returning
null. Look at the comment on Workbench#getActiveWorkbenchWindow().

If you call the method using anything other than the UI thread, it
returns null. It works sometimes, because you're probably calling it
from handlers being invoked in the UI thread.

You need to wrap the call in a Display.syncExec() block, i.e.

PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
public void run() {

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActivePart()...
}
});

HTH,

Wayne

On Mon, 2008-04-28 at 06:10 +0000, Allan Ramirez wrote:
> Hi,
>
> I have a view and in the constructor of my view, I have this line
>
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActivePart()...
> (ignore the remaining :D)
>
> Now, when the view is open when I lunch eclipse, it throws
> NullPointerException because getActivePage() returns null.
>
> It happens randomly. Sometimes, it's null sometimes not. How should I deal
> it? Please help.
>
>
> Cheers,
> Allan
>
Re: getActivePage throws NullPointerException [message #327765 is a reply to message #327643] Wed, 30 April 2008 19:10 Go to previous message
Eclipse UserFriend
If at all possible, within your view never go global.

1) get any services from your site
2) get your page and window from your site.

Your best bet is probably to try:
getSite().getPage().getActivePart()

The part may be null, but your page certainly will be real.

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm
Previous Topic:Specifying *BOTH* Lable and Icon in Toolbar MenuContribution
Next Topic:Bind a hot key combination without contributing to menus
Goto Forum:
  


Current Time: Sun Apr 27 23:34:28 EDT 2025

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

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

Back to the top