问题
I am trying to load the FXML file and show it as an application window, but i get an exception. The FXML file was created by the FXML Scene Builder.
Sup.java file:
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 file:
<?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>
screenshot of structure:
error message:
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
回答1:
The root cause of the exception you are getting is a NullPointerException with the detail message "Location is required.".
Caused by: java.lang.NullPointerException: Location is required.
I am not going to guide you how to read stack traces, please refer to this question instead.
You are getting the "location is required" error because you are passing null into the FXMLLoader.load() method. The null value must obviously come from the getResource() call. From the Class.getResource() documentation:
[...]
Returns:
A URL object ornullif no resource with this name is found[...]
So the reason why you are getting the exception has to be that the resource with the given name cannot be found.
Please validate the following:
- If you are using the standard eclipse project layout: Make sure you placed the
sample.fxmlinto the same directory as theSup.javafile If you are using a maven-like directory structure where there are different directories for resources and sources, make sure the
sample.fxmlis in the resource folder and in the same directory like the source file.Example: Your source file
Sup.javais placed insrc/main/java/com/example/Sup.java. Place thesample.fxmlfile intosrc/main/resources/com/example/sample.fxml.If you are using eclipse, clean and rebuild your project. Build tools have similar alternatives (or just delete the
build/outdirectory)
来源:https://stackoverflow.com/questions/36250965/exception-in-application-start-method