0

Object value in class becomes zero inside FXML annotated button action method.

I have an object declared in my class as follows,

private LoginData loginData;

From another Stage I am calling the method switchStage(Stage, LoginData) to initiate this stage. The method is,

public void switchStage(Stage primaryStage, LoginData lDat) throws Exception{
        if(lDat!= null) {
            loginData = lDat;
            System.out.println("SwitchStage : " + lDat.getUserAccessType());
        } else {
            loginData  = new LoginData();
        }

        start(primaryStage);
    }

In the start(Stage) method, I am loading the stage from an FXML file. The method definition is as follows,

public void start(Stage primaryStage) throws Exception {
        System.out.println("Before FXML load : " + loginData.getUserAccessType());
        Parent root = FXMLLoader.load(getClass().getResource("/com/<package>/view/LoadFiles.fxml"));
        System.out.println("After FXML Load : " + loginData.getUserAccessType());
        Scene scene = new Scene(root);
        root.setStyle("-fx-border-color: black; -fx-border-width: 1px; ");

        if (!primaryStage.isShowing())
            primaryStage.initStyle(StageStyle.UNDECORATED);

        scene.getStylesheets().add(ReconcileinaLoadFilesController.class.getResource("/com/<package>/view/LoadFiles.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.centerOnScreen();
        System.out.println("Before Stage show : " + loginData.getUserAccessType());
        primaryStage.show();
        System.out.println("After Stage show : " + loginData.getUserAccessType());
    }

I can see the value being retained in each of the above println statements.

I have a ToggleButton in my FXML file and the action method definition (in same class as the above methods) is as follows,

@FXML
public void settingsBtnAction() throws IOException{
    if(loginData != null) {
        System.out.println("Settings: " + loginData.getUserAccessType());
    } else {
        System.out.println("loginData is null"); // <--- code returns this everytime.
    }
}

What is happening here and how do I retain the value of the object loginData in the ToggleButton's action method? I am completely lost.

Edit:

I tried initializing the object in the line of declaration and it looks like it is retaining the value from here, but its not able to get the value that I've set manually.

private LoginData loginData = new LoginData(); //Gets me the value set in the constructor of the LoginData class.
iMan
  • 456
  • 1
  • 7
  • 18
  • 2
    Are you using the same class for the controller and for the `Application` subclass? I can't see how `loginData` would be initialized in the controller. – James_D Aug 30 '16 at 10:13
  • @James_D All these methods are in the Controller class (defined in FXML), this class (Stage) is started from another class, say Caller,java, Caller.java extends Application, not this controller class. – iMan Aug 30 '16 at 10:25
  • @James_D I tried making it extend Application subclass now as well, didn't make any difference. Not sure if you were suggesting to try it, but I did anyway. And, it would be helpful if you can guide me to how initialization can be done in controller. – iMan Aug 30 '16 at 10:26
  • 1
    See [this question](http://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class). Basically - you have two instances of your controller class - one on which you have set `loginData`, and a second one which was automatically created by `FXMLLoader`. The linked question explains this in more detail. – Itai Aug 30 '16 at 10:32
  • @sillyfly Thank you. I am facing issues when I try using the solution from there (to load FXML controller class manually without having the entry of fx:controller in fxml file). The issue is that my buttons (which have actions defined in the FXML file, onAction="#methodName") throw 'Error on resolving action=#methodName'. – iMan Aug 30 '16 at 11:18
  • @sillyfly Even after reading the answer, I am not sure how I am getting 2 instances created here. FXMLLoader.load() creates the only instance that I can see from the code and this is because of the fx:controller entry in the FXML file. I am seriously missing some understanding here. Any help would be appreciated. – iMan Aug 30 '16 at 11:24
  • 1
    `FXMLLoader.load()` creates a new instance. You obviously already have one instance, because otherwise you would be able to call `start()`, from where you call `FXMLLoader.load()`. The structure is really unclear and you should [edit] the question to make it clear which classes the methods are defined in, and which classes are controllers for which FXML files. – James_D Aug 30 '16 at 12:56
  • Actually... the real problem is: you already seem to have an instance of the controller before you call `FXMLLoader.load()`, because (somehow) you are calling `FXMLLoader.load()` *from inside the controller* (???). Where does that instance come from? Unless you are loading the same FXML file twice, there should be no way that can happen. – James_D Aug 30 '16 at 13:27
  • @James_D Thanks for responding, I will update the question with more details once I have access to the system the code is on. – iMan Aug 31 '16 at 10:01

0 Answers0