将 null 从 JavaScript 传递到 Java 时 JxBrowser 出现 NullPointerException

NullPointerException in JxBrowser when passing null from JavaScript to Java

以下代码在 JxBrowser(版本 6.14)java 代码中导致 NullPointerException,当将 null 作为参数传递给 java 方法时,我无法进一步调试该代码.

在文档中它说 null 转换为 null,所以我认为它应该工作并且它是一个错误:https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript(table 在底部)

有什么想法如何 "fix" 这个吗?

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserPreferences;
import com.teamdev.jxbrowser.chromium.JSObject;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.events.ConsoleEvent;
import com.teamdev.jxbrowser.chromium.events.ConsoleListener;
import com.teamdev.jxbrowser.chromium.events.ScriptContextEvent;
import com.teamdev.jxbrowser.chromium.events.ScriptContextListener;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import java.awt.BorderLayout;
import java.util.logging.Level;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class SSCCE_JxBrowser {

    public static void main(String[] args) {
        LoggerProvider.setLevel(Level.INFO); // ALL
        BrowserPreferences.setChromiumSwitches("--remote-debugging-port=9222");

        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setTitle("JxBrowser Test");
        frame.setVisible(true);

        browser.addScriptContextListener(new ScriptContextListener() {

            @Override
            public void onScriptContextDestroyed(ScriptContextEvent event) {}

            @Override
            public void onScriptContextCreated(ScriptContextEvent event) {
                JSObject window = event.getBrowser().executeJavaScriptAndReturnValue("window").asObject();
                window.setProperty("java", new JavaObject());
            }

        });

        browser.addConsoleListener(new ConsoleListener() {

            @Override
            public void onMessage(ConsoleEvent event) {
                System.out.println("log: " + event);
            }

        });

        browser.loadHTML("<html><head>" //
                + "<script>" //
                + "java.log('Ok!');" // this is OK
                + "java.log(null);" // this fails with NullPointerException
                + "</script>" //
                + "<body>page loaded - <a href=\"" + browser.getRemoteDebuggingURL() + "\" target=_blank>debug</a>"
                + "</body></html>");
    }

    public static class JavaObject {

        public void log(String msg) {
            System.out.println("msg: " + msg);
        }

    }

}

使用 异常断点 我发现了这个堆栈跟踪:

Thread [IPC Sync Events Thread] (Suspended (exception java.lang.NullPointerException))  
    com.teamdev.jxbrowser.chromium.JSContext.a(com.teamdev.jxbrowser.chromium.JSContext, com.teamdev.jxbrowser.chromium.internal.ipc.message.OnInvokeJSJavaMessage) line: 2608  
    com.teamdev.jxbrowser.chromium.JSContext$a.onMessageReceived(com.teamdev.jxbrowser.chromium.internal.ipc.message.Message) line: 280 
    com.teamdev.jxbrowser.chromium.internal.ipc.p.a(com.teamdev.jxbrowser.chromium.internal.ipc.p, com.teamdev.jxbrowser.chromium.internal.ipc.message.Message) line: 1082  
    com.teamdev.jxbrowser.chromium.internal.ipc.q.run() line: 66    
    com.teamdev.jxbrowser.chromium.internal.q.run() line: 63    
    java.util.concurrent.Executors$RunnableAdapter<T>.call() line: 511  
    java.util.concurrent.FutureTask<V>.run() line: 266  
    java.util.concurrent.ThreadPoolExecutor.runWorker(java.util.concurrent.ThreadPoolExecutor$Worker) line: 1142    
    java.util.concurrent.ThreadPoolExecutor$Worker.run() line: 617  
    java.lang.Thread.run() line: 748    

此问题已在 download 可用的 JxBrowser 6.19.2-b1-eap 版本中得到修复。该修复程序将包含在下一个正式版本中。

欢迎您下载包含修复程序的 JxBrowser 6.20。