0

I am using the following code to create a JavaScript bridge to my HTML file.

final JavaLink link = new JavaLink();
htmlView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        @Override
        public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
            if (t1 == Worker.State.SUCCEEDED) {
                JSObject js = (JSObject) webEngine.executeScript("window");
                js.setMember("app", link);
            }
        }
    });
webEngine = htmlView.getEngine();
webEngine.load(getClass().getResource("../html/src.html").toString());
webEngine.setUserStyleSheetLocation(getClass().getResource("../html/style.css").toString());

to execute the following JavaScript when I click on a html object

app.notificationClick(event.target.id.toString());

The JavaLink class has not much except the called method.

public class JavaLink {
    public void notificationClick(String cssId) {

    }

}

The problem I am having is here. I have a TextArea with a

onKeyReleased() -> setElement(KeyEvent event)

public void setElement(KeyEvent event) {    
     webEngine.getDocument().getElementById(id).setTextContent(inputArea.getText());
}

and whenever I write something into the TextArea everything works except when I pressed the HTML Object to fire the JavaScript before I enter something into the TextArea. This results in the following error:

Exception in thread "JavaFX Application Thread" 
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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.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$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(GlassViewEventHandler.java:248)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 35 more
Caused by: java.lang.NullPointerException
at mainwindow.MainWindowController.setElement(MainWindowController.java:29)
... 45 more

TL;DR

Whenever my HTML file fires it's JavaScript via the JavaLink class, the JavaFX Thread stops working for my ChangeListener on the TextArea and can't change GUI Elements.

Selensija
  • 21
  • 2
  • You have a null pointer exception. This has nothing at all to do with threading. What is actually null? – James_D Nov 20 '17 at 23:18
  • @James_D Well the error happens at: `webEngine.getDocument().getElementById(id).setTextContent(inputArea.getText());` But everything is initiated and works unless I clicked the html Object beforehand. When I comment out the JavaLink everything works as well... That's why I kept thinking it has to do something with the JavaFX thread. – Selensija Nov 20 '17 at 23:21
  • So either the document, or the element you get, or possibly the input area is null. Why don't you figure out which? – James_D Nov 20 '17 at 23:24
  • @James_D Okay thank you a lot I was a bit blinded by other things and would never thought it was actually the Document's fault becoming `null` – Selensija Nov 20 '17 at 23:39

0 Answers0