JavaFX应用程序在启动时抛出NullPointerException
更新:即使我删除对JavaFX应用程序在启动时抛出NullPointerException,java,eclipse,nullpointerexception,javafx-8,Java,Eclipse,Nullpointerexception,Javafx 8,更新:即使我删除对fxmloader类的调用,也会出现相同的问题。即使使用空的start()方法,也会引发异常。这个问题根本与目录树结构无关 对于我启动的每个JavaFX项目,我总是遇到启动时抛出NullPointerExceptions的问题。即使使用了显示阶段所需的最基本代码,也会发生这种情况。我可以用以下代码复制该问题: Main.java import javafx.application.Application; import javafx.fxml.FXMLLoader; impor
fxmloader
类的调用,也会出现相同的问题。即使使用空的start()
方法,也会引发异常。这个问题根本与目录树结构无关
对于我启动的每个JavaFX项目,我总是遇到启动时抛出NullPointerException
s的问题。即使使用了显示阶段所需的最基本代码,也会发生这种情况。我可以用以下代码复制该问题:
Main.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1">
</AnchorPane>
启动时的堆栈跟踪:
Thread [main] (Suspended (exception NullPointerException))
SystemProperties.setVersions() line: not available [local variables unavailable]
SystemProperties.lambda$static$28() line: not available
314337396.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]
SystemProperties.<clinit>() line: not available
LauncherImpl.startToolkit() line: not available
LauncherImpl.launchApplicationWithArgs(String, String, String[]) line: not available
LauncherImpl.launchApplication(String, String, String[]) line: not available
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[main](挂起(异常NullPointerException))
SystemProperties.setVersions()行:不可用[局部变量不可用]
SystemProperties.lambda$static$28()行:不可用
314337396.run()行:不可用
AccessController.doPrivileged(PrivilegedAction)行:不可用[本机方法]
系统属性。()行:不可用
LauncherImpl.startToolkit()行:不可用
LaunchImpl.launchApplicationWithArgs(字符串,字符串,字符串[])行:不可用
LaunchImpl.launchApplication(字符串、字符串、字符串[])行:不可用
NativeMethodAccessorImpl.invoke0(方法、对象、对象[])行:不可用[本机方法]
NativeMethodAccessorImpl.invoke(对象,对象[])行:不可用
DelegatingMethodAccessorImpl.invoke(对象,对象[])行:不可用
调用(对象,对象…)行:不可用
LaunchHelper$FXHelper.main(字符串…)行:不可用
如果我在那之后恢复执行:
Thread [JavaFX Application Thread] (Suspended (exception NullPointerException))
PropertyHelper.lambda$getBooleanProperty$510(String) line: not available
712544800.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]
PropertyHelper.getBooleanProperty(String) line: not available
Parent.<clinit>() line: not available
NativeConstructorAccessorImpl.newInstance0(Constructor<?>, Object[]) line: not available [native method]
NativeConstructorAccessorImpl.newInstance(Object[]) line: not available
DelegatingConstructorAccessorImpl.newInstance(Object[]) line: not available
Constructor<T>.newInstance(Object...) line: not available
Class<T>.newInstance() line: not available
ReflectUtil.newInstance(Class<?>) line: not available
FXMLLoader$InstanceDeclarationElement.constructValue() line: not available
FXMLLoader$InstanceDeclarationElement(FXMLLoader$ValueElement).processStartElement() line: not available
FXMLLoader.processStartElement() line: not available
FXMLLoader.loadImpl(InputStream, Class<?>) line: not available
FXMLLoader.loadImpl(Class<?>) line: not available
FXMLLoader.loadImpl(URL, ResourceBundle, BuilderFactory, Callback<Class<?>,Object>, Charset, Class<?>) line: not available
FXMLLoader.loadImpl(URL, ResourceBundle, BuilderFactory, Callback<Class<?>,Object>, Class<?>) line: not available
FXMLLoader.loadImpl(URL, ResourceBundle, BuilderFactory, Class<?>) line: not available
FXMLLoader.loadImpl(URL, ResourceBundle, Class<?>) line: not available
FXMLLoader.loadImpl(URL, Class<?>) line: not available
FXMLLoader.load(URL) line: not available
Main.start(Stage) line: 10
LauncherImpl.lambda$launchApplication1$159(AtomicBoolean, Application) line: not available
1666080238.run() line: not available
PlatformImpl.lambda$runAndWait$172(Runnable, CountDownLatch) line: not available
972765878.run() line: not available
PlatformImpl.lambda$null$170(Runnable) line: not available
1842446646.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]
PlatformImpl.lambda$runLater$171(Runnable, AccessControlContext) line: not available
1651945012.run() line: not available
InvokeLaterDispatcher$Future.run() line: not available
WinApplication._runLoop(Runnable) line: not available [native method]
WinApplication.lambda$null$145(Runnable) line: not available
2091156596.run() line: not available
Thread.run() line: not available
Thread[JavaFX应用程序线程](挂起(异常NullPointerException))
PropertyHelper.lambda$getBooleanProperty$510(字符串)行:不可用
712544800.运行()行:不可用
AccessController.doPrivileged(PrivilegedAction)行:不可用[本机方法]
PropertyHelper.getBooleanProperty(字符串)行:不可用
父项。()行:不可用
NativeConstructorAccessorImpl.newInstance0(构造函数,对象[])行:不可用[本机方法]
NativeConstructorAccessorImpl.newInstance(对象[])行:不可用
DelegatingConstructorAccessorImpl.newInstance(对象[])行:不可用
构造函数.newInstance(对象…)行:不可用
Class.newInstance()行:不可用
reflectil.newInstance(类)行:不可用
FXMLLoader$InstanceDecrationElement.constructValue()行:不可用
FXMLLoader$InstanceDeclarationElement(FXMLLoader$ValueElement)。processStartElement()行:不可用
FXMLLoader.processStartElement()行:不可用
FXMLLoader.loadImpl(InputStream,类)行:不可用
FXMLLoader.loadImpl(类)行:不可用
FXMLLoader.loadImpl(URL、ResourceBundle、BuilderFactory、回调)行:不可用
FXMLLoader.loadImpl(URL、ResourceBundle、BuilderFactory、回调)行:不可用
FXMLLoader.loadImpl(URL、ResourceBundle、BuilderFactory、类)行:不可用
FXMLLoader.loadImpl(URL、ResourceBundle、类)行:不可用
FXMLLoader.loadImpl(URL,类)行:不可用
FXMLLoader.load(URL)行:不可用
主启动(阶段)线:10
LaunchImpl.lambda$launchApplication1$159(AtomicBoolean,应用程序)行:不可用
1666080238.run()行:不可用
PlatformImpl.lambda$runAndWait$172(可运行,倒计时)行:不可用
972765878.run()行:不可用
PlatformImpl.lambda$null$170(可运行)行:不可用
1842446646.run()行:不可用
AccessController.doPrivileged(PrivilegedAction,AccessControlContext)行:不可用[本机方法]
PlatformImpl.lambda$runLater$171(可运行,AccessControlContext)行:不可用
1651945012.运行()行:不可用
invokelateDispatcher$Future.run()行:不可用
WinApplication.\u runLoop(Runnable)行:不可用[本机方法]
WinApplication.lambda$null$145(可运行)行:不可用
2091156596.运行()行:不可用
Thread.run()行:不可用
问题是,如果我再次继续执行,程序就会正常启动。我的阶段显示正确,所有资源加载没有问题等等。我真的无法想象JavaFX会故意抛出NullPointerException
s,如果一切按预期进行。我遗漏了什么
我正在使用运行在Windows8.1上的EclipseLuna(4.4.2)
这些项目是使用JavaSE-1.8和JavaFXSDK(我认为是JavaFX8)构建的。验证Main.fxml文件是否位于src/Main/resources/folderWhy
FXMLLoader
不强制执行特定的目录树,是吗?此外,这并没有解决问题,也没有做任何更改。文件是相对于类文件进行搜索的,代码应该是父root=fxmloader.load(getClass().getResource(“/Main.fxml”);这也没有任何区别。我的问题不是没有找到fxml文件,程序启动正常,只是先抛出异常。还有,看看更新我会分析它,看看我是否能找出为什么会发生这种情况,你的错误,这真的很有趣。