以下代码在JxBrowser(版本6.14)java代码中导致NullPointerException,当将null
作为参数传递给java方法时,我无法进一步调试。
在文档中说null
转换为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);
}
}
}
,所以我认为它应该有效并且它是一个错误:https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript(表格在底部)
任何想法如何解决&#34;此?
Uncaught java.lang.NullPointerException: null
msg: Ok!
log: ConsoleEvent{lineNumber=1, message='Uncaught java.lang.NullPointerException: null', source='about:blank'}
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
使用异常断点,我找到了这个堆栈跟踪:
{{1}}
答案 0 :(得分:1)
此问题已在download的JxBrowser 6.19.2-b1-eap版本中修复。修复程序将包含在下一个正式版本中。
答案 1 :(得分:1)
欢迎您下载包含修复程序的JxBrowser 6.20。