我正在尝试加载FXML文件并将其显示为应用程序窗口,但我得到一个例外。 FXML文件由FXML Scene Builder创建。
Sup.java文件:
package sup;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Sup extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Sup");
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml文件:
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sup.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="5">
<Text text="Sup"
GridPane.rowIndex="0"
GridPane.columnSpan="2"
GridPane.halignment="CENTER"/>
<Label text="First Name:"
GridPane.rowIndex="1"
GridPane.columnIndex="0"/>
<TextField
GridPane.columnIndex="1"
GridPane.rowIndex="1"/>
<Button text="Say Sup!"
GridPane.rowIndex="2"
GridPane.columnIndex="1"
GridPane.halignment="RIGHT"/>
</GridPane>
结构截图:
错误消息:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at sup.Sup.start(Sup.java:34)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Exception running application sup.Sup
答案 0 :(得分:3)
您获得的例外的根本原因是NullPointerException
,其中包含详细信息&#34;位置是必需的。&#34;。
Caused by: java.lang.NullPointerException: Location is required.
我不会指导您如何阅读堆栈跟踪,请参阅this question。
您需要&#34;位置是必需的&#34;错误,因为您将null
传递给FXMLLoader.load()
方法。 null
值显然必须来自getResource()
来电。来自Class.getResource()
文档:
[...]
<强>返回:强>
如果找不到具有此名称的资源,则为URL
对象或null
[...]
因此,您获得异常的原因必须是找不到具有给定名称的资源。
请验证以下内容:
sample.fxml
放入与Sup.java
文件相同的目录如果您使用 maven-like目录结构,其中有不同的资源和来源目录,请确保sample.fxml
位于资源文件夹中且位于同一目录中像源文件一样的目录。
示例:您的源文件Sup.java
位于src/main/java/com/example/Sup.java
。将sample.fxml
文件放入src/main/resources/com/example/sample.fxml
。
如果您正在使用eclipse,请清理并重建您的项目。构建工具有类似的替代方案(或只删除build
/ out
目录)