views:

620

answers:

2

Using LiveConnect getMember(String) method of a window JSObject, a Java object of type "Object" is returned but I don't find the expected members defined on it. Neither can I cast it to a JSObject to continue using the getMember().

Accessing DOM nodes and elements (DOM Objects) works as expected.

A: 

@Michael Borgwardt

This is what I'm trying to do.

Global JavaScript Object.

foo = {"one":1, "two":2};

Processing code.

import netscape.javascript.*;
JSObject win;
JSObject got1;
Object   got2;

void setup(){
  size(400,200,P2D);
  background(255);
  win  = JSObject.getWindow(this); //gets the root JSObject
  //got1 = win.getMember("obj"); //fails to cast from Object to JSObject
  got2 = win.getMember("obj"); //works in the sense that I get an Object.
}

void draw(){
  if (mousePressed == true){
    println(got2.getMember("one")); //fails, there is no getMember() method.
  }
}

This is perhaps not Processing specific. That's why I didn't originally post the code.

A: 

I'm confused by the line got2 = win.getMember("obj");. It seems like you're expecting that line to get the foo object. As I understand it, win.getMember("obj") will get you the obj property on the DOM window node, which doesn't exist. For example, from this page:

JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject loc = (JSObject) doc.getMember("location");

If you want to get the foo object, the best way I'm aware of is to make a JavaScript method you can call from the Java applet.

JavaScript:

<script type="text/javascript">
var foo = {"one":1, "two":2};

function getFoo() { return foo; }
</script>

Applet:

import netscape.javascript.*;
JSObject win;
JSObject foo;

void setup(){
  size(400,200,P2D);
  background(255);
  win  = JSObject.getWindow(this); //gets the root JSObject
  foo = (JSObject)win.call("getFoo", null);
}

void draw(){
  if (mousePressed == true){
    println(foo.getMember("one"));
  }
}
Matthew Maravillas
MDC says that it's supposed to be a "JavaScript Object". Whatever that means.https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/LiveConnect/JSObject
Yes, but I don't get why you expect it to get the "foo" object. "JavaScript object" just means it's a object from JavaScript that's been translated to a Java object, specifically JSObject.
Matthew Maravillas
Did that work for you? Because it's giving me an exception.
It did. What's your exception?
Matthew Maravillas
Exception in thread "Animation Thread" java.lang.NullPointerException at js_processing_api.draw(js_processing_api.java:34) at processing.core.PApplet.handleDraw(PApplet.java:1406) at processing.core.PApplet.run(PApplet.java:1311) at java.lang.Thread.run(Thread.java:619)
Random guess: try clearing your classloader cache? Bring up your Java console (Tools - Java console in FF, or right click on the Java icon in your systray and pick the console), and hit x to clear it, then reload.
Matthew Maravillas
I did try that. It throws the error when I click on the applet.
I'm not sure I have any more suggestions for you, except to be pedantic - are you using the exact code I wrote above? I can get the exception if I have getFoo return null, but my line number is 32 instead of 34, so I suspect you have something else there as well.
Matthew Maravillas
It works now. Thanks.. I don't know what the error was but I copy pasted your code and viola it works. Thanks a ton.
There's another problem, how may I assign it the values I get to a variable. The type appears to be an Object and I can't cast it to float or string.