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

时间:2016-05-16 23:08:16

标签: java javafx nullpointerexception javafx-2

我试图访问加载fxml的窗口的控制器类的实例。它加载时没有错误,但是当我尝试使用

打印帐户数时
System.out.println("from NAW: "+ NAC.newAccModel.users.getNumAccs());

它在下面给出了nullpointer异常。 (第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();

    }

}

2 个答案:

答案 0 :(得分:0)

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

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

root = fxmlLoader.load();

答案 1 :(得分:0)

 NAC = (newAccountController) fxmlLoader.getController();
 System.out.println("from NAW: "+NAC.newAccModel.users.getNumAccs());

你不能在getController之后执行sysout。您正在做的是,您在控制器newAccModel方法中创建或初始化initilize。 intialize花了一些时间来接听电话。它会在加载完成时调用。但

 NAC = (newAccountController) fxmlLoader.getController();

在这一行之后,没有准备好装载。所以它只是给NPE。请确保在控制器中执行初始化方法后访问newAccModel