Getter正确返回,然后更改为null

时间:2017-03-22 23:36:11

标签: java javafx setter getter

所以我遇到了一个不寻常的问题,即代码使用方法getCurrentUser()检测当前用户,但是当我稍后使用它来检查哪个用户借了一本书时它返回null。

我还尝试将borrowBook方法的第3行更改为currentUser变量,并尝试通过usernameTxtField.getText()获取信息

public class Controller {
private Book HFJ = new Book("HFJ", "SI & BA","5678",3, 1);

@FXML
private void attemptLogin(ActionEvent event) throws IOException {
    String usernameInput = usernameTxtField.getText();
    String passwordInput = passwordTxtField.getText();
    boolean loginSuccessful = false;


    switch (usernameInput.toLowerCase()) {
        case "user1":
            if (passwordInput.equals("Password1")) {
                System.out.println("Login Successful");
                informationLabel.setText("Login Successful");
                loginSuccessful = true;
                currentUser = usernameInput.toLowerCase();
            }
            break;
        case "user2":
            if (passwordInput.equals("Password2")) {
                System.out.println("Login Successful");
                informationLabel.setText("Login Successful");
                loginSuccessful = true;
                currentUser = usernameInput.toLowerCase();
            }
            break;
        case "user3":
            if (passwordInput.equals("Password3")) {
                System.out.println("Login Successful");
                informationLabel.setText("Login Successful");
                loginSuccessful = true;
                currentUser = usernameInput.toLowerCase();
            }
            break;
        case "user4":
            if (passwordInput.equals("Password4")) {
                System.out.println("Login Successful");
                informationLabel.setText("Login Successful");
                loginSuccessful = true;
                currentUser = usernameInput.toLowerCase();
            }
            break;
        default:
            System.out.println("Incorrect username or Password");
            informationLabel.setText("Incorrect username or Password");
            break;
    }

    if (loginSuccessful == true) {
        Parent home = FXMLLoader.load(getClass().getResource("home.fxml"));
        Scene home_scene = new Scene(home);
        Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        primaryStage.setScene(home_scene);
        primaryStage.show();
        currentUser = usernameInput.toLowerCase();
        System.out.println(getCurrentUser());
        homeLoginInfoLabel.setText("Login Successful");
    }
}
@FXML private Label HFJQuantity = new Label();
@FXML private Button HFJBorrowBtn = new Button();
@FXML
private void borrowBook(ActionEvent event) throws IOException{
    if(HFJ.getQuantity()>=1){
        HFJ.setQuantity(HFJ.getQuantity() -1);
        System.out.println("Book Borrowed By: " + getCurrentUser());//CONSOLE PRINT
        HFJQuantity.setText("" + HFJ.getQuantity());
        Parent borrowSuccessful = FXMLLoader.load(getClass().getResource("borrowSuccess.fxml"));
        Scene success_scene = new Scene(borrowSuccessful);
        Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        primaryStage.setScene(success_scene);
        primaryStage.show();
    }else{
        System.out.println("Not enough books");
        HFJBorrowBtn.setDisable(true);
    }

}


private String getCurrentUser() {
    return currentUser;
}

当我运行代码并登录时,这就是控制台打印的内容:

Login Successful
user1
Book Borrowed By: null

为什么它不识别当前用户?任何帮助都会很棒

1 个答案:

答案 0 :(得分:1)

我认为你遇到的问题属于范围。我在这个类中看不到你在switch语句或if / else之外声明了currentUser。尝试使用其他类变量在顶部初始化currentUser。