2017-10-10 113 views
-1

我正在爲我的程序創建一個基本的登錄窗口,我似乎無法切換場景沒有程序給我一個「NullPointerException」錯誤。 代碼:JavaFX NullPointerException與fxml

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(); 

} 

這是我使用切換場景的方法,登錄按鈕是第一個場景按鈕,使用它,以獲得第一現場1m,並從獲得的窗口。

最後,這就是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方法的空指針錯誤。 非常感謝!

+4

什麼確切的線是拋出NullPointerException?包括堆棧跟蹤。該文件是否與定義了'switchScenes'的類存在於同一個包中? – f1sh

+0

準確的行:Scene scene = loginButton.getScene();在switchScene方法中。是的,我的程序中的所有文件都存在於同一個數據包中。和堆棧跟蹤:產生的原因:在sample.Main.switchScenes(Main.java:35)顯示java.lang.NullPointerException \t \t在sample.Controller.verifyLogin(Controller.java:35) 感謝@ f1sh –

+0

因此'loginButton '爲空。請參閱https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it –

回答

0

像@James_D說,問題是 -

的FXMLLoader初始化@控制器FXML標註的領域。你不是在控制器上調用switchScenes(...),而是在你創建的其他對象上調用它。因此loginButton沒有被初始化,並且仍然是空的。 - James_D

所以我只是將swtichScene方法移入Controller類,並且它工作。

0

試試下面的代碼:

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是很重要的。