首页 > 解决方案 > ControlsFX 通知中的 NullPointerException

问题描述

我想在我的 JavaFX 应用程序中向用户显示通知,并为此选择了 ControlsFX 库。但是,每当我通过: 创建警告时Notifications.create().showWarning();,我都会收到以下异常:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at org.controlsfx.controls/org.controlsfx.control.Notifications$NotificationPopupHandler.getScreenBounds(Notifications.java:364)
    at org.controlsfx.controls/org.controlsfx.control.Notifications$NotificationPopupHandler.show(Notifications.java:343)
    at org.controlsfx.controls/org.controlsfx.control.Notifications.show(Notifications.java:305)
    at org.controlsfx.controls/org.controlsfx.control.Notifications.showWarning(Notifications.java:271)

当我的整个应用程序设置并可见时调用它。

这可能是版本不匹配吗?我使用 JavaFX 12,但 ControlsFX 只有版本 11。

这是我的缩写版本pom.xml

<project xmlns="[...]">
    [...]
    <properties>
        <maven.compiler.release>12</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>eu.snik.tag.gui.Main</mainClass>
                </configuration>
            [...]
            </plugin>
        </plugins>
              [...]
    <dependencies>
    [...]       
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>12.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>11.0.0</version>
        </dependency>
    </dependencies>
</project>

标签: javafxnullpointerexceptioncontrolsfx

解决方案


这是一个现已修复的错误,请参阅https://github.com/controlsfx/controlsfx/issues/1140

不幸的是,当前版本中不包含错误修复(或任何候选版本:根据版本历史,它已于 2019 年 4 月 30 日修复,但最后一个版本是在同月 19 日)。

请注意,防止 NPE 的检查之前的代码null是通过Window.getWindows()查找聚焦窗口返回的窗口列表迭代添加的。null如果没有找到这样的窗口,则使用。使用当前代码,如果没有找到焦点窗口,则使用主屏幕来确定通知的位置。


推荐阅读