Reputation: 13
I'm trying to access an instance of the controller class for the window that loads the fxml. It loads with no errors, but when I try to print the number of accounts using
System.out.println("from NAW: "+ NAC.newAccModel.users.getNumAccs());
it is giving the nullpointer exception below. (line 36 is the 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)
here is where I am trying to call the 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);
}
here is the controller if it matters
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();
}
}
Upvotes: 1
Views: 1060
Reputation: 3140
NAC = (newAccountController) fxmlLoader.getController();
System.out.println("from NAW: "+NAC.newAccModel.users.getNumAccs());
you cannot to do the sysout
after the getController. what you are doing is, you creating or initializing the newAccModel
in the controller initilize
method.
intialize took sometime to gets call. it will call when load complete. but
NAC = (newAccountController) fxmlLoader.getController();
after this line, there was load not ready. so its simply giving NPE. please make sure you are access the newAccModel
after the initialize method executed in the controller
Upvotes: 0
Reputation: 82451
By passing an URL
to the load
method, you use one of the static
load
methods of FXMLLoader
, which of course cannot store the controller in your FXMLLoader
instance since there is no information about the instance available.
Since you already specify the URL
in the FXMLLoader
constructor, simply use the parameterless load
method instead:
root = fxmlLoader.load();
Upvotes: 1