简体   繁体   中英

NPEs with JavaFX's HTMLEditor

I wrote a relatively simple test class:

public class Main extends Application {

    @Override public void start(Stage stage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 400, 400);

        HTMLEditor editor = new HTMLEditor();
        root.setCenter(editor);

        Platform.runLater(editor::requestFocus);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

If I run this (the editor has focus), I am greeted by this rather bleak looking window, and if I type anything, I immediately get a NullPointerException :

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.javafx.scene.web.skin.HTMLEditorSkin.applyTextFormatting(HTMLEditorSkin.java:1100)
    at com.sun.javafx.scene.web.skin.HTMLEditorSkin.lambda$new$3(HTMLEditorSkin.java:293)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
...

在此处输入图片说明

The three things I noticed at first were:

  1. The error seems to be related to HTMLEditorSkin , so I tried applying one but Eclipse informs me this class is not API. How can I use this (if I should) and if yes, should I make an access restriction (since JavaFX itself is not API either)?
  2. The HTMLEditor looks unfocused, and has no selected font by default (I think this is where the problem is being caused.)
  3. The HTMLEditorSkin is from the package hierarchy com.sun.javafx and not javafx .

However, it seems that I am doing everything correctly according to the first example of Oracle's tutorial .

What is going wrong here? It feels like this may be a bug.


Attempted solutions and already-received answers:

  1. Setting initial text with setHtmlText .
    setHtmlText("<body style='font-family: something;'>text</body>");
    It does set the text and a font but the editor is unaffected.
  2. Set style with setStyle .
    setStyle("-fx-font: 12 something");
    This only changes the UI font.
  3. Installing JDK 8u122.

The error is raised because due to a bug, HtmlEditor does not set a font family by default and when you start typing there are no fonts selected so you'll get NPE . Notice that if you select a font family before starting to type, no error is raised.

In order to avoid this bug upgrade to JDK 8u122 . (Tested on Kubuntu 16.10 with jre-8u122-ea-bin-b04-linux-x64-25_oct_2016 )

PS I did not check previous versions. This could have been fixed in earlier releases but it exists in 8u111 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM