views:

53

answers:

1

Hi, I am trying to call javascript method from an Applet using netscapte.java.JSObject.

in the applet:

JSObject window = JSObject.getWindow(this.Class); 
Object[] args = ....  //arguments 
window.call("javascriptMethodName", args); 

But I get the exception at window.call:

JavaScript error while calling "callFromJava"
netscape.javascript.JSException: JavaScript error while calling "callFromJava"
    at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
    at TextBoxApplet.jButton1_actionPerformed(TextBoxApplet.java:57)
    at TextBoxApplet.access$000(TextBoxApplet.java:16)
    at TextBoxApplet$1.actionPerformed(TextBoxApplet.java:36)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

The JSObject is NOT null. Have anyone encountered this ? Thanks a lot.

+1  A: 

I did a quick test and my findings correlate well with musicfreak's comment.

With the following Applet:

public class MyClass extends JApplet {
    public void init() {
        JSObject window = JSObject.getWindow(this); 
        Object[] args = new String[] { "bar" }; 
        window.call("foo", args); 
    }
}

and the following markup (notice the erroneous JavaScript - there's no baz method in the bar argument I'm passing):

<html>
  <head>
    <title>MyAppletTest</title>
  </head>
  <body>
    <script type="text/javascript">
      function foo(bar) { bar.baz(); }
    </script>
    <applet code="MyApplet.class"></applet>
  </body>
</html>

I get the error:

netscape.javascript.JSException: JavaScript error while calling "foo"
    at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
    at MyApplet.init(MyApplet.java:13)

If I fix my JavaScript function by replacing bar.baz() with alert(bar) for example, everything works fine.

Long story short - take another look at the JavaScript function you are trying to call, as well as the parameters your Applet passes with the call.

Lauri Lehtinen
Hi Lauri, thanks for your help. I'll check it again to see if anything wrong with the js.
Khue Vu