Jonathan Beaudoin
Jonathan Beaudoin

Reputation: 2188

WebView NullPointerException using JavaFX

Recently adding a WebView pane to my JavaFX Application. When resizing my program I get this error and the program freezes.

java.lang.NullPointerException
    at com.sun.javafx.webkit.prism.WCPageBackBufferImpl.validate(WCPageBackBufferImpl.java:97)
    at com.sun.webkit.WebPage.paint(WebPage.java:644)
    at com.sun.javafx.sg.prism.NGWebView.renderContent(NGWebView.java:95)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
    at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2067)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
    at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
    at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2308)
    at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2202)
    at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2228)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2061)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:235)
    at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:576)
    at com.sun.javafx.sg.prism.NGNode.renderForClip(NGNode.java:2308)
    at com.sun.javafx.sg.prism.NGNode.renderRectClip(NGNode.java:2202)
    at com.sun.javafx.sg.prism.NGNode.renderClip(NGNode.java:2228)
    at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2061)
    at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1959)
    at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:474)
    at com.sun.javafx.tk.quantum.ViewPainter.paintImpl(ViewPainter.java:327)
    at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:91)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
    at java.lang.Thread.run(Thread.java:745)

The website that the webview goes to is

http://www.abendigo.org/gui/

Can someone tell me what causes this issue?

Edit: Not sure why this is needed, but here is the code as requested:

public class Controller implements Initializable {

    @FXML
    public WebView webView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        webView.getEngine().load("http://abendigo.org/gui/");
    }

}

Video of bug reproduction: https://youtu.be/6CLPt45AbW0

Upvotes: 1

Views: 2063

Answers (1)

Daniel
Daniel

Reputation: 3923

I was faced with the same bug. It was quite difficult to find the reason. At last, I found : webView layout throw a nullPointerExpection when width or height is too large, ie over 8500 pixel.

And the good bypass was too limit the maxWidth/maxHeight of the webView to scene size :

 webView.sceneProperty().addListener(new ChangeListener<Scene>() {

        @Override
        public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene scene) {
            if (scene != null) {
                webView.setMaxSize(getScene().getWidth(), getScene().getHeight());
                webView.maxWidthProperty().bind(getScene().widthProperty());
                webView.maxHeightProperty().bind(getScene().heightProperty());
            } else {
                webView.maxWidthProperty().unbind();
                webView.maxHeightProperty().unbind();
            }
        }
    });

Upvotes: 4

Related Questions