异常 运行 应用程序 sample.Main

Exception running application sample.Main

我正在用 JavaFX 制作一个 ToDo 应用程序,但它显示了这个错误(即使没有数据库,登录系统也只是想 运行 它很明显): 我喜欢编码,但不喜欢出现错误 这就是为什么我被称为 ErrorLane

Main.java:

package sample;

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("C:\Users\arhaan\eclipse-workspace\ToDoApp\src\sample\viewlogin.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

login.fxml:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" fx:controller="application.FXMLDocumentController" alignment="center" hgap="10" vgap="10">
</GridPane>

它在 Eclipse 而不是 IntelliJ IDEA 和 JRE 1.8.0

这是错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    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(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
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 sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(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(WinApplication.java:186)
    ... 1 more
Exception running application sample.Main

“错误”是尝试使用绝对路径作为 FXMLLoader 的参数引起的 NPE。 FXMLLoader 采用 URL,因此要将您的绝对路径转换为可以传递给 FXMLLoader 的 URL,请使用 FileInputStream,或转换为 URL:

文件输入流:

 Parent root = FXMLLoader.load(new FileInputStream("C:\Users\arhaan\eclipse-workspace\ToDoApp\src\sample\login.fxml"));

转换为URL:

Parent root = FXMLLoader.load(Paths.get("C:\Users\arhaan\eclipse-workspace\ToDoApp\src\sample\login.fxml").toUri().toURL());

这回答了您的问题,但这是糟糕的设计。为您的源使用适当的结构,并在同一包的 class 路径中使用 .fxml,直接与控制器一起使用,或者在包含相同包布局的专用资源目录中。使用硬编码的绝对路径,您的代码不可移植,并且需要其他任何人在他们的系统上具有与 运行 您的代码相同的确切路径。