我試圖訪問加載fxml的窗口的控制器類的實例。它加載沒有錯誤,但是當我嘗試打印使用的帳戶數時fxmlLoader.getController()導致空指針異常?
System.out.println("from NAW: "+ NAC.newAccModel.users.getNumAccs());
它在下面給出空指針例外。 (第36行是println
)
java.lang.NullPointerException
at muselogin.newAccountWindow.<init>(newAccountWindow.java:36)
at muselogin.MuseLoginController.initialize(MuseLoginController.java:84)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at muselogin.MuseLogin.start(MuseLogin.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
這裏就是我想打電話給getController()
public class newAccountWindow extends Application {
Stage stage = new Stage();
newAccountController NAC = new newAccountController();
public newAccountWindow(){
Parent root=null;
try{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("newAccountWindow.fxml"));
root = fxmlLoader.load(getClass().getResource("newAccountWindow.fxml"));
// fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
NAC = (newAccountController) fxmlLoader.getController();
System.out.println("from NAW: "+NAC.newAccModel.users.getNumAccs());
}catch(Exception e){
e.printStackTrace();
}
Scene scene = new Scene(root);
//scene.getStylesheets().add(MuseLogin.class.getResource("newAccCSS.css").toExternalForm());
stage.setScene(scene);
}
這裏控制器,如果它的事項
public class newAccountController implements Initializable {
newAccountModel newAccModel;
@FXML
private TextField usernameField;
@FXML
private PasswordField passwordField;
@FXML
private Button createAccount;
@FXML
private PasswordField confirmField;
@FXML
private Label usernameBlankMessage;
@FXML
private Label usernameTakenMessage;
@FXML
private Label passwordMessage;
//counter to check if all 3 conditions are met
int makeAcc = 0;
//action event for make account button clicked
@FXML
private void createAccountClicked(ActionEvent event) {
//does account creation stuff
}
public int getNumAccs(){
return newAccModel.users.getNumAccs();
}
//required initialize function, initializes model
@Override
public void initialize(URL url, ResourceBundle rb) {
newAccModel = new newAccountModel();
}
}
'newAccountModel()'的構造函數有什麼作用?您不提供代碼。它是否將用戶與'newAccountModel'相關聯,並將參考存儲在'newAccountModel'的'users'成員中?如果不是,那可能是你的NullPointerException的源頭:'users'爲null,所以'users.getItems()'拋出一個NullPointerException異常。請參閱:[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i -fix-it)請遵循[class命名約定](http://www.iwombat.com/standards/JavaStyleGuide.html)。 – jewelsea