2017-05-31 44 views
0

以下代碼在JxBrowser(版本6.14)java代碼中導致NullPointerException,因此在將null作爲參數傳遞給java方法時,我無法進一步調試。JxBrowser中的NullPointerException在將JavaScript從JavaScript傳遞給Java時發生null

在本文檔中,它說null轉換爲null,所以我想它應該工作,這是一個錯誤:https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript(表在底部)

任何想法如何「修復」呢?

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); 
     } 

    } 

} 
  • 輸出(HTML):
    • Uncaught java.lang.NullPointerException: null
  • 輸出(JAVA):
    • 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

你認爲沒有傳遞'null'嗎? –

+0

''java.log(null);「 //當NullPointerException失敗時......你傳遞null ...... – evolutionxbox

+0

我想將'null'傳遞給我的java對象。 'NullPointerException'應該只在嘗試執行'null.method()'之類的時候纔會發生。再加上文檔說'null'轉換爲'null',所以它似乎被支持。再說一遍:JFX瀏覽器(它是JxBrowser的替代品)確實支持將'null'傳遞給java。在JS代碼方法調用中傳遞'null'也是沒有問題的。我相信這是一個錯誤。 –

回答

0

此問題已被固定在供download JxBrowser 6.19.2-B1-EAP構建。該修補程序將包含在下一個正式版本中。