簡體   English   中英

帶有 fxml 的 JavaFX NullPointerException

[英]JavaFX NullPointerException with fxml

我正在為我的程序創建一個基本的登錄窗口,如果程序沒有給我一個“NullPointerException”錯誤,我似乎無法切換場景。 代碼:

public class Controller {

@FXML
private javafx.scene.control.TextField usernameTextField, passwordTextField;
@FXML
private Label loginFailed;

public static void main(String[] args) {

}

@FXML
private void verifyLogin() throws IOException { // Verifying the both the username, and password exists in the database
    Configure obj = new Configure();
    Main obj1 = new Main();
    String username = usernameTextField.getText();
    String password = passwordTextField.getText();

    try {
        Scanner scanner = new Scanner(obj.file);

        //now read the file line by line...
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if(line.contains(username) && line.contains(password)) {
                System.out.println("Half Way");
                obj1.switchScenes("userHomescreen.fxml");
                System.out.println("True");
                return;
            }
        }
    } catch(FileNotFoundException e) {
        System.out.println("ERROR! File not found!");
    }
    loginFailed.setText("Wrong Username or Wrong Password!");
}

}

這是第一個場景的控制器類。 我的程序鏈接到一個文件,我正在使用掃描儀類並檢查密碼和用戶名。

@FXML Button loginButton
void switchScenes(String sceneName) throws IOException {
    Scene scene = loginButton.getScene();
    Window window = scene.getWindow();
    Stage stage = (Stage) window;

    Parent primaryStage = FXMLLoader.load(getClass().getResource(sceneName));
    Scene primaryScene = new Scene(primaryStage);
    stage.setScene(primaryScene);
    stage.show();

}

這就是我用來切換場景的方法,登錄按鈕是第一個場景上的按鈕,我使用它是為了獲取第一個場景,然后從中獲取窗口。

最后,這是 fxml 代碼:

    <GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <Pane fx:id="loginPane" prefHeight="540.0" prefWidth="395.0">
         <children>
            <Label layoutX="22.0" layoutY="91.0" text="Username:" />
            <Label layoutX="22.0" layoutY="178.0" text="Password" />
            <TextField fx:id="usernameTextField" layoutX="22.0" layoutY="122.0" prefHeight="25.0" prefWidth="351.0" />
            <TextField fx:id="passwordTextField" layoutX="22.0" layoutY="204.0" prefHeight="25.0" prefWidth="351.0" />
            <Button fx:id="loginButton" layoutX="22.0" layoutY="271.0" mnemonicParsing="false" onAction="#verifyLogin" prefHeight="25.0" prefWidth="130.0" text="Login" />
            <Label fx:id="loginFailed" layoutX="25.0" layoutY="240.0" prefHeight="17.0" prefWidth="351.0" />
         </children></Pane>
   </children>
</GridPane>

所以回到我的問題,請幫助我理解並修復 switchScenes 方法中發生的 NullPointer 錯誤。 非常感謝!

就像@James_D 說的,問題是——

FXMLLoader 初始化控制器中帶有@FXML 注釋的字段。 您不是在控制器上調用 switchScenes(...),而是在您創建的其他對象上調用它。 因此 loginButton 未初始化並且仍然為空。 - 詹姆斯_D

所以我只是將 swtichScene 方法移到 Controller 類中,並且它起作用了。

試試下面的代碼:

public void switchScenes(String sceneName){
    try {      
        Stage stage = loginButton.getScene().getWindow();

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource(sceneName));
        Parent root = loader.load();
        Scene primaryScene = new Scene(root);
        stage.setScene(primaryScene);                   
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Failed to Open Scene");            
        ex.getCause();
    }
}

注意:字符串sceneName應該是“/PackageName/FXMLfile.fxml”

PackageName 之前的“/”很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM