执行webemgine.executescript时java.lang.reflect.InvocationTargetException

执行webemgine.executescript时java.lang.reflect.InvocationTargetException,java,exception,webview,javafx,invocationtargetexception,Java,Exception,Webview,Javafx,Invocationtargetexception,我正在使用JavaFX突出显示网络视图中的文本。但是当我运行代码时;出现以下异常情况。有人能给我一个解决方案吗 提前谢谢。 编辑:我在下面添加了完整的错误堆栈。正如我所建议的那样 @FXML private void _do_highlight(WebEngine engine, String _text) { engine.executeScript("$('body').removeHighlight().highlight('" + _text + "')"); } @FXML

我正在使用JavaFX突出显示网络视图中的文本。但是当我运行代码时;出现以下异常情况。有人能给我一个解决方案吗

提前谢谢。 编辑:我在下面添加了完整的错误堆栈。正如我所建议的那样

@FXML
private void _do_highlight(WebEngine engine, String _text) {
  engine.executeScript("$('body').removeHighlight().highlight('" + _text + "')");
}    
@FXML
private void hcone(MouseEvent event) {
    str = one.getText();  // 'one' is a Textfield ... 
    if (submited == true) {    
        WebEngine engine = webview.getEngine();  
        engine.loadContent("<body><div id='content'>Hello asdfasdfasdf</div></body>");
        _do_highlight(engine, str); // Each time str is found in webview, it must be highlighted
    }
}
基于例外情况

netscape.javascript.JSException:ReferenceError:找不到变量:$

您似乎没有正确加载jQuery

如果您了解了这一点,那么必须首先加载jQuery,然后执行脚本

这对我来说毫无例外(尽管内容没有突出显示,脚本也不起作用……):

@FXML private-WebView-WebView;
@FXML私有文本字段一;
@凌驾
公共void初始化(URL、ResourceBundle rb){
}    
@FXML
私有无效hcone(ActionEvent事件){
String str=one.getText();
如果(!str.isEmpty()){
WebEngine=webView.getEngine();
loadContent(“Hello asdfasdfasdf”);
engine.documentProperty().addListener(
(道具、旧道具、新道具)->突出显示(引擎、str);
}
}
私有void\u do\u突出显示(WebEngine引擎,字符串\u文本){
ExecuteQuery(引擎“$”(\“body\”)。removeHighlight()。highlight(“+\u text+”);”);
}
其中,
executejQuery()
是该答案中引用的方法,该方法检查Web引擎中加载的文档是否具有与加载的最低所需版本相对应的jQuery版本,如果没有,则从默认jQuery位置将jQuery加载到文档中


请注意,我添加了一个侦听器,因为函数要求在执行脚本注入jQuery之前将文档加载到WebView中。

如果它是从控制器调用的私有方法,则似乎
\u do\u highlight()
不应该有
@FXML
注释。另外,您应该显示异常的完整堆栈,以便我们可以找到错误的位置。。结果是一样的。。现在给出了完整的异常堆栈。。
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3437)
at javafx.scene.Scene$ClickGenerator.access$7900(Scene.java:3365)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3733)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at     com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at              com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/584634336.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
... 30 more
Caused by: netscape.javascript.JSException: ReferenceError: Can't find variable: $
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1426)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:948)
at bioassignment.StringOccurenceController._a_highlight(StringOccurenceController.java:83)
at bioassignment.StringOccurenceController.hcone(StringOccurenceController.java:96)
... 40 more
@FXML private WebView webView;

@FXML private TextField one;

@Override
public void initialize(URL url, ResourceBundle rb) {
}    

@FXML
private void hcone(ActionEvent event){
    String str = one.getText();
    if(!str.isEmpty()){
        WebEngine engine = webView.getEngine();
        engine.loadContent("<body><div id='content'>Hello asdfasdfasdf</div></body>");
        engine.documentProperty().addListener(
            (prop, oldDoc, newDoc) -> _do_highlight(engine, str));
    }
}

private void _do_highlight(WebEngine engine, String _text) {
    executejQuery(engine,"$(\"body\").removeHighlight().highlight('" + _text + "');");
}