3
votes

I perused the other threads on that topic but still I cannot get my code to work hence seeking help.

Here is the code:

    BrowserComponent bc = new BrowserComponent();
    bc.setDebugMode(true);
    bc.setPage("<html><body></body></html>", null);
    JavascriptContext context = new JavascriptContext(bc);
    bc.addWebEventListener("onLoad", e -> {
        {
            JSObject syso = (JSObject) context.get("{}");
            syso.set("print", new JSFunction() {
                @Override
                public void apply(JSObject self, Object[] args) {
                    System.out.println(StringUtils.injectArgs("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}", args));
                }
            });
            context.set("window.syso", syso);
        }

        {
            JSObject syso = (JSObject) context.get("syso");
            syso.call("print", "I", "am", "in", "syso");
        }
    });

And here is the error:

    Error trying to execute js if (typeof(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1) == 'undefined'){ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1=[]};(ca_weblite_codename1_js_JavascriptContext_RETURN_VAR_call_3=(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso'))
[EDT] 0:0:0,2 - Codename One revisions: 0576ed169fa3bf9f9fe32b3ad516f3aeee60a66c

[EDT] 0:0:0,3 - Exception: java.lang.RuntimeException - Failed to execute javascript ca_weblite_codename1_js_JavascriptContext_RETURN_VAR_call_3=(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso').  The error was netscape.javascript.JSException: TypeError: 'undefined' is not a function (evaluating '(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso')')
java.lang.RuntimeException: Failed to execute javascript ca_weblite_codename1_js_JavascriptContext_RETURN_VAR_call_3=(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso').  The error was netscape.javascript.JSException: TypeError: 'undefined' is not a function (evaluating '(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[0]).call(ca_weblite_codename1_js_JavascriptContext_LOOKUP_TABLE1[1], 'print', 'I', 'am', 'in', 'syso')')
    at com.codename1.javascript.JavascriptContext.call(JavascriptContext.java:975)
    at com.codename1.javascript.JavascriptContext.call(JavascriptContext.java:862)
    at com.codename1.javascript.JavascriptContext.call(JavascriptContext.java:807)
    at com.codename1.javascript.JSObject.call(JSObject.java:960)
    at net.etceterum.app.javascript.JSEngine.lambda$0(JSEngine.java:105)

         //JSEngine.java:105 = syso.call("print", "I", "am", "in", "syso");

at net.etceterum.app.javascript.JSEngine$$Lambda$9/30625616.actionPerformed(Unknown Source)
    at com.codename1.ui.util.EventDispatcher.fireActionSync(EventDispatcher.java:459)
    at com.codename1.ui.util.EventDispatcher.access$100(EventDispatcher.java:45)
    at com.codename1.ui.util.EventDispatcher$CallbackClass.run(EventDispatcher.java:95)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1114)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1058)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:946)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

What am I doing wrong?

Thanks for any help you can provide.

1
Steve might be more qualified to answer that but it looks to me like syso might be undefined at this stage - Shai Almog

1 Answers

0
votes

The problem is that you are trying to call "syso" as a function, with parameters "print", "I", "am", "in", "syso". In JS, what you've done is

window.syso('print','I','am','in','syso');

Change

syso.call("print", "I", "am", "in", "syso");

to

syso.call("print", new Object[]{"I", "am", "in", "syso"});

And it will work.