|
|
Re: getActivePage throws NullPointerException [message #327652 is a reply to message #327647] |
Mon, 28 April 2008 08:08 |
Allan Ramirez Messages: 14 Registered: July 2009 |
Junior Member |
|
|
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 13:17 |
Wayne Beaton Messages: 554 Registered: December 2017 |
Senior Member |
|
|
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
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.03061 seconds