0

I'm trying to build a register/login system using JavaFX, and I'm having a "Register a new account" button within the login form. My intention is to redirect the user to the registration page if he clicks on that button. The registration page shouldn't open in a new window, but within the same window as the login form.

This is the structure of my login-view.fxml file:

<VBox id="root" fx:id="rootPane" alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.yuber.controllers.LoginController">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>

    <VBox id="paneSmall" alignment="BASELINE_CENTER">
        <Label id="title">Yuber</Label>
        <TextField id="input" fx:id="username">Username</TextField>
        <PasswordField id="input" fx:id="password">Password</PasswordField>
        <Button id="btn-default-small" onAction="#onLoginButtonClick" text="Login" />
        <Hyperlink onAction="#onRegisterButtonClick">Register a new account</Hyperlink>
        <Label id="error" fx:id="error" />
    </VBox>
</VBox>

As you can see, the parent tag is a VBox. I've found several answers on this website, all of them containing an AnchorPane as a parent element, but I'm pretty sure using a VBox won't affect everything that much and would most likely have the same effect.

Inside my controller, I have this line:

@FXML
private VBox rootPane;

and also this is the #onRegisterButtonClick event:

@FXML
protected void onRegisterButtonClick(ActionEvent event) throws IOException {
    VBox pane = FXMLLoader.load(getClass().getResource("register-view.fxml"));
    rootPane.getChildren().setAll(pane);
}

The problem is, my app throws a NullPointerException:

Caused by: java.lang.NullPointerException: Location is required.

Yes. The register-view.fxml file is inside my resources folder. It looks like that: enter image description here

Also tried replacing the loader to use getClass().getClassLoader().getResource(...), and also Main.class.getClass().getClassLoader().getResource(...), but nothing seemed to fix it. The same error.

Mario Mateaș
  • 185
  • 1
  • 12
  • 1
    Please edit your question to show where your classes are located (not just the resources). If the class that you are making the call from is nested within a package/folder then `getClass().getResource("register-view.fxml")` will only work as a relative path. So you could try VBox pane = XMLLoader.load(Main.class.getClassLoader().getResource("register-view.fxml")); See here for more info: [What is the difference between Class.getResource() and ClassLoader.getResource()?](https://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource) – sorifiend May 20 '22 at 04:54

0 Answers0