首页 > 解决方案 > java.lang.NullPointerException:需要位置。javafx

问题描述

我正在尝试从路径src/interfaces/panels/中的 fxml 文件创建一个 Scene 类,该文件名为StartUp.fxml,但出现此错误:

java.lang.NullPointerException: Location is required.
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
        at tienda.TIENDA.start(TIENDA.java:30)
public void start(Stage stage) throws Exception {
        window=stage;
        String path = new File("src/interfaces/panels/StartUp.fxml").getAbsolutePath();
        System.out.println(path);//I added this just in case
        Parent root = FXMLLoader.load(getClass().getResource("src/interfaces/panels/StartUp.fxml"));//this is the problem
        su=new Scene(root);
        window.setScene(su);
        window.show();
    }

编辑:这是解决方案 TIENDA是类,路径是相对于类的

public void start(Stage stage) throws Exception {
        window=stage;
        var resource = TIENDA.class.getResource("../src/interfaces/panels/StartUp.fxml");//HERE
        System.out.println(resource);
        Parent root = FXMLLoader.load(resource);
        su=new Scene(root);
        window.setScene(su);
        window.show();
    }

标签: java

解决方案


推荐阅读