[英]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
: 如果运行此命令(编辑器将焦点放在编辑器上),则会看到此窗口看起来非常暗淡,如果输入任何内容,我会立即收到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: 我最初注意到的三件事是:
HTMLEditorSkin
, so I tried applying one but Eclipse informs me this class is not API. 该错误似乎与HTMLEditorSkin
有关,因此我尝试应用一个,但Eclipse通知我该类不是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)? 我该如何使用它(如果应该),如果可以,我应该设置访问限制(因为JavaFX本身也不是API)? HTMLEditor
looks unfocused, and has no selected font by default (I think this is where the problem is being caused.) HTMLEditor
看起来没有重点, 默认情况下没有选定的字体 (我认为这是引起问题的地方。) HTMLEditorSkin
is from the package hierarchy com.sun.javafx
and not javafx
. HTMLEditorSkin
来自包层次结构com.sun.javafx
而不是javafx
。 However, it seems that I am doing everything correctly according to the first example of Oracle's tutorial . 但是,看来我按照Oracle教程的第一个示例正确地完成了所有操作。
What is going wrong here? 这是怎么了? It feels like this may be a bug. 感觉这可能是一个错误。
Attempted solutions and already-received answers: 尝试的解决方案和已经收到的答案:
setHtmlText
. 使用setHtmlText
设置初始文本。 setHtmlText("<body style='font-family: something;'>text</body>");
setStyle
. 用setStyle
设置样式。 setStyle("-fx-font: 12 something");
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
. 出现此错误是因为由于存在错误, HtmlEditor
默认不设置字体系列,并且在您开始键入时没有选择字体,因此您将获得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 . 为了避免此错误,请升级到JDK 8u122 。 (Tested on Kubuntu 16.10
with jre-8u122-ea-bin-b04-linux-x64-25_oct_2016
) (在Kubuntu 16.10
使用jre-8u122-ea-bin-b04-linux-x64-25_oct_2016
)
PS I did not check previous versions. PS我没有检查以前的版本。 This could have been fixed in earlier releases but it exists in 8u111
. 这可能已在较早的发行版中修复,但存在于8u111
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.