2

my problem is with JavaFX.

I've created Login Panel (first scene) and when someone put correct login and password and click the button -> the scene should be changed to another one.

Everything compiles properly but when click the button it throws exception. (It is something about invoke or something) but in the end there is something like that:

Caused by: java.lang.NullPointerException
at Login.LoginController.Login(LoginController.java:50)

So the problem is in my Controller, in method Login, here it is:

@FXML
public void Login(MouseEvent event) throws SQLException, IOException {

    DatabaseConnection polaczenie = new DatabaseConnection();
    polaczenie.selectDataFromDB("select * from logowanie");

    if(tfLogin.getText().equals(polaczenie.getLogin()) && pfPassword.getText().equals(polaczenie.getPassword())) {
            lblStatus.setText("Udalo sie zalogowac.");

        Parent window1;
        window1 = FXMLLoader.load(getClass().getResource("/Menu/Menu.fxml"));

        Scene newSceneWindow1 = new Scene(window1);

        Stage mainStage;
        //mainStage = (Stage)  ((Node)event.getSource()).getScene().getWindow();
        mainStage = Main.getPrimaryStage();
        mainStage.setScene(newSceneWindow1);
        mainStage.show();
    }
    else {
        lblStatus.setText("Logowanie nie powiodlo sie.");
    }

And two lines with problem:

mainStage.setScene(newSceneWindow1);
mainStage.show();

When i delete these two, everything compiles properly BUT it doesn't change scene.

I don't know how to repair this, I checked almost everything.

PS

My Main:

private static Stage primaryStage;

private void setPrimaryStage(Stage stage) {
    Main.primaryStage = stage;
}

static public Stage getPrimaryStage() {
    return Main.primaryStage;
}

@Override
public void start(Stage primaryStage) throws Exception{
    FXMLLoader firstPaneLoader = new FXMLLoader(getClass().getResource("LoginPanel.fxml"));
    Parent firstPane = firstPaneLoader.load();
    Scene firstScene = new Scene(firstPane);

    primaryStage.getIcons().add(new Image("file:C:///Users/Bartek/Desktop/HarcerzSystem-master/test.png"));
    primaryStage.setTitle("Harcerz System");
    primaryStage.setScene(firstScene);
    primaryStage.show();
}

public static void main(String[] args) {

    launch(args);
}

0 Answers0