1

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

    }

}
fabian
  • 80,457
  • 12
  • 86
  • 114
Chase
  • 13
  • 4
  • What does the constructor for the `newAccountModel()` do? You don't provide code for it. Does it associate users with a `newAccountModel` and store a reference in the `users` member of `newAccountModel`? If not, that is probably the source of your NullPointerException: `users` is null so `users.getItems()` throws a NullPointerException. See: [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) Please follow [class naming conventions](http://www.iwombat.com/standards/JavaStyleGuide.html). – jewelsea May 17 '16 at 01:40

2 Answers2

1

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();
fabian
  • 80,457
  • 12
  • 86
  • 114
  • This worked like a charm! Why would having a second declaration of the url cause this to return a null controller? – Chase May 17 '16 at 18:17
  • @Chase: Writing `fxmlLoader.load(url);` has the same effect as writing `FXMLLoader.load(url)`. Since that load method is `static`, there is no information available that would allow the method to associate the controller instance it creates with the `FXMLLoader` instance you're (not) using. If you want to access properties of the `FXMLLoader` like the controller (assuming it's created during the loading), you need to use one of the non-`static` `load` methods. – fabian May 18 '16 at 14:12
0
 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

subash
  • 3,116
  • 3
  • 18
  • 22