javafx - 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>
解决方案
这是一个现已修复的错误,请参阅https://github.com/controlsfx/controlsfx/issues/1140
不幸的是,当前版本中不包含错误修复(或任何候选版本:根据版本历史,它已于 2019 年 4 月 30 日修复,但最后一个版本是在同月 19 日)。
请注意,防止 NPE 的检查之前的代码null
是通过Window.getWindows()
查找聚焦窗口返回的窗口列表迭代添加的。null
如果没有找到这样的窗口,则使用。使用当前代码,如果没有找到焦点窗口,则使用主屏幕来确定通知的位置。
推荐阅读
- indexing - 返回值不是升序的
- flutter-dependencies - Flutter - 获取当前字幕(索引)。更好的球员
- go - 在不同结构之间共享方法实现
- python - 将字符串分成两部分
- spring-webflux - Spring WebClient 下载和上传文件
- stm32 - MCU 上有两个 UART:需要(或明智)FreeRTOS?
- react-native - ios 模拟器 14.5 版上 Yogakit 的致命错误
- google-cloud-platform - 将数据从 GCE VM 实例下载到本地 PC - 它是出口数据吗?
- kubernetes - 使用 ArgoCD CLI 添加新部署集群的问题
- r - 如何使用 mockery 存根 S4 类方法