0

I'm new to JavaFX. I've created a JavaFX program and it worked well until something(idk what) happened. For example My main function load start menu and it works well

public void start(Stage stage) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("startMenu.fxml"));
    Scene scene = new Scene(fxmlLoader.load(),956,573);
    stage.setTitle("Taxes");
    stage.setScene(scene);
    stage.show();
}

But when I click button register I get this errors:

java.lang.NullPointerException: Location is required.
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Root cannot be null

And this is my changeScene function

public static void changeScene(ActionEvent event, String fxmlFile, String title,String name,String last_name){
    Parent root = null;
    if(name!=null&&last_name!=null){
        try{
            FXMLLoader loader = new FXMLLoader(DBUtils.class.getClass().getResource(fxmlFile));
            root = loader.load();
            LogInController logInController = loader.getController();
            logInController.setUserInfo(name,last_name);
        }catch(Exception e){
            e.printStackTrace();
        }
    }else{
        try{
            root=FXMLLoader.load(DBUtils.class.getClass().getResource(fxmlFile));
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    Stage stage =(Stage) ((Node) event.getSource()).getScene().getWindow();
    stage.setTitle("Taxes");
    stage.setScene(new Scene(root, 956,573));
    stage.show();
}

I've tried to move fxml files out of resources but nothing has changed. Method changeScene I've done by tutorial and it worked perfectly

  • 1
    `DBUtils.class.getClass().getResource` -> drop the `getClass()` before the getResource. Ensure that whatever you are looking up is in the correct relative location to the DBUtils class. See [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) – jewelsea Nov 28 '22 at 02:59

0 Answers0