gpt4 book ai didi

java - fxmlLoader.getController() 导致空指针异常?

转载 作者:行者123 更新时间:2023-11-30 07:22:46 25 4
gpt4 key购买 nike

我正在尝试访问加载 fxml 的窗口的 Controller 类的实例。它加载时没有错误,但是当我尝试使用打印帐户数量时

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

如果重要的话,这是 Controller

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

}

}

最佳答案

通过将 URL 传递给 load 方法,您可以使用 FXMLLoaderstatic load 方法之一,这当然无法将 Controller 存储在您的 FXMLLoader 实例中,因为没有有关该实例的可用信息。

由于您已经在 FXMLLoader 构造函数中指定了 URL,因此只需使用无参数 load 方法即可:

root = fxmlLoader.load();

关于java - fxmlLoader.getController() 导致空指针异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37264610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com