According to this bug report attempting to create a Canvas
that is too large for the graphics system fails "silently" by only dumping a NullPointerException
stack trace to the console. However, in my application the canvas size can be based on user input so I need to detect this. But since the NPE is caught in a background JavaFX thread, we can't rely on it to detect the issue. Any idea how I could programmatically detect that the Canvas creation failed, from within the application thread?
Results will vary based on hardware, but a large enough size should exercise the problem
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
Button button = new Button("Create large canvas");
HBox hbox = new HBox(button);
button.setOnAction(e -> {
hbox.getChildren().add(new Canvas(500,50000));
// Did I get an NPE or can the Canvas render?
});
stage.setScene(new Scene(hbox));
stage.show();
}
}
In my case this leads to (as visible on the console):
java.lang.NullPointerException
at javafx.graphics/com.sun.javafx.sg.prism.NGCanvas$RenderBuf.validate(NGCanvas.java:213)
at javafx.graphics/com.sun.javafx.sg.prism.NGCanvas.initCanvas(NGCanvas.java:641)
at javafx.graphics/com.sun.javafx.sg.prism.NGCanvas.renderContent(NGCanvas.java:604)
at javafx.graphics/com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:2072)
at javafx.graphics/com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1964)
...