Monday, November 25, 2013

Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance

Thanks ChrisK, You saved my day.

On Tuesday, October 23, 2012 3:23:21 PM UTC+2, ChrisK wrote:
There were a few problems here. My first example calls a zero args function but is not formatted correctly.

If you call something like this:
  this.@com.mine.PlayerImpl::callback();

It won't work. It needs the method signature and parameters separated (no args signature doesn't imply no args last time I checked in GWT):
  this.@com.mine.PlayerImpl::callback()();

Notice the additional set of brackets. Very briefly mentioned on https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI.

For the final code, I ended up passing "this" as a parameter from Java and also wrapping the JS callback function so it ends up looking like this:

  public PlayerImpl() {
    initialiseCallbacks(this);
  }

  private native void initialiseCallbacks(PlayerImpl impl) /*-{
    $wnd.js_callback = $entry(function() {
      impl.@com.mine.PlayerImpl::callback()();
    });
  }

I hope that helps.


On Monday, 22 October 2012 15:23:43 UTC+1, Kartik Kaipa wrote:
Hey
did you finally get this working.
I am doing the exact same thing
I pass in the instance of the object as a parameter in the function method

Could you post the working code that you have.
Would really apreciate it.

Thanks,
Kartik

On Tuesday, March 23, 2010 7:31:24 PM UTC+5:30, Olivier Monaco wrote:
ChrisK,

For instance method, you need to reference the method from the
instance like "this.@...". Here is my exact code that compile:

private native void installListener()
/*-{
    var callback = $entry(this.
@net.antidot.gwtcommon.mvp.share.client.channel.HTML5MessageChannel::process(Lnet/
antidot/gwtcommon/mvp/share/client/channel/HTML5MessageEvent;));
    $wnd.addEventListener("message", (function(callback, channel) {
        return function(event) {
            callback.call(channel, event);
        };
    })(callback, this), false);;
}-*/;

Sorry, I forgot that...

Olivier

On 23 mar, 13:58, ChrisK <cknow...@gmail.com> wrote:
> Actually, Olivier's methods work in development mode but don't seem to
> compile with this error:
>
> "Cannot make an unqualified reference to the instance method callback"
>
> I've tried the pre-assignment of "this" to "var that" and also an
> argument to the JSNI method passing in my instance. I've tried both of
> these with Olivier's first method as well as the second to avoid
> leaks. All with the same result - working in development mode but not
> when I compile.
>
> It looks to me as though the compiler doesn't understand the call
> method or the closure correctly. It looks at though it's trying to
> parse the @mypackage.MyClass::MyMethod() and seeing it's an instance
> method so failed (instead of looking at subsequent JavaScript). Of
> course I could quite easily be doing something wrong.
>
> Thomas - your method compiles fine so I guess I'll use that for now.
>
> On Mar 23, 10:07 am, ChrisK <cknow...@gmail.com> wrote:
>
> > Ahhh ok. Sorry for the simple questions but I'm just getting my head
> > around all this. I also have some other methods with arguments and
> > have those working thanks to both of you.
>
> > On Mar 23, 9:56 am, Olivier Monaco <olivier.mon...@free.fr> wrote:
>
> > > ChrisK,
>
> > > On 23 mar, 10:32, ChrisK <cknow...@gmail.com> wrote:
>
> > > > Thomas - your solution worked from 6 worked but at first I didn't
> > > > include both pairs of brackets after the function call "MyMethod" but
> > > > it turns out they are required. With only one set (i.e. no arguments),
> > > > it just doesn't work. I thought that was legal but maybe not.
>
> > > Remember that "@mypackage.MyClass::MyMethod()" is a reference to the
> > > function, not a call. The parenthesis allow you to give the full
> > > signature of the method. Java allows two methods to have the same name
> > > but different parameters. GWT needs the "full" signature to find which
> > > method you want to reference, even is there is only one method with
> > > this name.
>
> > > The second parenthesis are to call the method. If you forgot then, you
> > > just obtain the function object and do nothing with it...
>
> > > Olivier
>
>

a\@a

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment