【发布时间】:2017-11-24 20:38:13
【问题描述】:
我运行的每个 JavaFX 应用程序都会引发两个 NullPointerException。它们不会阻止甚至影响项目的执行,而且只有在调试模式下运行我的应用程序时才能看到它们。我什至遇到了来自 Oracle 的 HelloWorld 示例和这个最小程序的问题:
public class JavaFXTSample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
StackPane iAmRoot = new StackPane();
Scene scene = new Scene(iAmRoot, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main (String[] args) {
launch(args);
}
}
这是第一个错误的堆栈跟踪:
Thread [main] (Suspended (exception NullPointerException))
SystemProperties.setVersions() line: 81 [local variables unavailable]
SystemProperties.lambda$static$28() line: 67
30621981.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]
SystemProperties.<clinit>() line: 64
LauncherImpl.startToolkit() line: 668
LauncherImpl.launchApplicationWithArgs(String, String, String[]) line: 337
LauncherImpl.launchApplication(String, String, String[]) line: 328
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available
Method.invoke(Object, Object...) line: not available
LauncherHelper$FXHelper.main(String...) line: not available
这是第二个:
Thread [JavaFX Application Thread] (Suspended (exception NullPointerException))
PropertyHelper.lambda$getBooleanProperty$514(String) line: 39
7164036.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]
PropertyHelper.getBooleanProperty(String) line: 37
Parent.<clinit>() line: 87
JavaFXTSample.start(Stage) line: 16
LauncherImpl.lambda$launchApplication1$162(AtomicBoolean, Application) line: 863
2266602.run() line: not available
PlatformImpl.lambda$runAndWait$175(Runnable, CountDownLatch) line: 326
32251660.run() line: not available
PlatformImpl.lambda$null$173(Runnable) line: 295
11305869.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]
PlatformImpl.lambda$runLater$174(Runnable, AccessControlContext) line: 294
30052382.run() line: not available
InvokeLaterDispatcher$Future.run() line: 95
WinApplication._runLoop(Runnable) line: not available [native method]
WinApplication.lambda$null$148(int, Runnable) line: 191
32126786.run() line: not available
Thread.run() line: not available
更重要的是,如果我删除了iAmRoot 和scene 的任何实例(所以start() 只是读取primaryStage.show();),第二个错误不会发生。为什么会这样?
我之前已经能够找到这个问题 (JavaFX application throws NullPointerException at startup),但似乎没有人解决它,并且在 2 多年前被问到。
如果有帮助,我在 Windows 7 Professional 上运行 Eclipse 4.5.2,我认为我根本没有使用 FXML。
编辑:
对于它的价值,我找不到第二个错误的源代码,但我找到了引发第一个错误的方法的 JavaFX 代码(第 81 行):
58 private static final String versionResourceName =
59 "/com/sun/javafx/runtime/resources/version.properties";
...
78 private static void setVersions() {
79 int size;
80 InputStream is =
81 SystemProperties.class.getResourceAsStream(versionResourceName);
82 try {
83 size = is.available();
84
85 byte[] b = new byte[size];
86 int n = is.read(b);
87 String inStr = new String(b, "utf-8");
88 SystemProperties.setFXProperty("javafx.version",
89 getValue(inStr, "release="));
90
91 SystemProperties.setFXProperty("javafx.runtime.version",
92 getValue(inStr, "full="));
93
94 } catch (Exception ignore) {
95 }
96 }
【问题讨论】:
-
现在又回来了。但它仍然作为图像发布,真的很难阅读......“Thread [Main](Suspended......)”很奇怪。你是在调试模式下运行的吗?
-
我将其重新格式化为代码,并在调试中运行它。这些错误根本不会得到处理,它们不会影响代码,因此查看它们的唯一方法就是在调试模式下运行。
-
那么如果你正常运行,它们不会出现在控制台中?
-
他们没有。除非它处于调试模式,否则就像永远不会发生错误一样。我想据我所知,它们只发生在调试模式下,但我认为情况并非如此。将对
launch()的调用封装在try-catch中也不会影响任何事情。 -
一个方法能带来多少代码异味? 1) 假设
InputStream.available()为总文件大小,2) 忽略read(…)的返回值,3) 不关闭InputStream,4) 捕获并忽略所有异常。讨论 5),考虑到所有这些其他气味,变量的不必要的广泛范围将毫无意义……
标签: java javafx nullpointerexception