0

I defined the following FirstWindow.fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="switcherContainer" layoutX="0.0" layoutY="0.0" prefHeight="170.0" prefWidth="200.0"
                xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
                fx:controller="rep.ButtonController">

                <fx:define>
                     <ToggleGroup fx:id="rType"/>  
                </fx:define>

                <Label layoutX="35.0" layoutY="25.0" prefHeight="17.0" prefWidth="260.0" text="Choose the report type:" />

                <RadioButton layoutX="35.0" layoutY="60.0"  mnemonicParsing="false" text="A" toggleGroup="$rType" />
                <RadioButton layoutX="35.0" layoutY="80.0"  mnemonicParsing="false" text="B" toggleGroup="$rType" />
                <RadioButton layoutX="35.0" layoutY="100.0" mnemonicParsing="false" text="C" toggleGroup="$rType" />
                <RadioButton layoutX="35.0" layoutY="120.0" mnemonicParsing="false" text="D" toggleGroup="$rType" />  
</AnchorPane>

I added this FirstWindow.fxml file in Main class:

AnchorPane rootPane = (AnchorPane) FXMLLoader.load(getClass().getResource("/rep/RootScene.fxml"));
primaryStage.setTitle("Report Generation");
primaryStage.setScene(new Scene(rootPane));     
AnchorPane innerPane = (AnchorPane) FXMLLoader.load(getClass().getResource("/rep/FirstWindow.fxml"));
rootPane.getChildren().addAll(innerPane);       
primaryStage.show();

And my ButtonController class looks like this:

public class ButtonController implements Initializable {
    static int step = 0;

    @FXML
    private Button nextButton;

    @FXML
    ToggleGroup rType;

    @Override
    public void initialize(URL location, ResourceBundle resources) {          
    }

    public void onNextButtonClick(ActionEvent event) {
         System.out.println("Button Clicked!");      
         try {              
             if (rType != null) {
                 System.out.println("RadioButton selected: " + rType.getSelectedToggle().getUserData().toString());
             } else {
                 System.out.println("rType is null");
             }       

             ...
             primaryStage.show();       
         } catch (Exception ex) {
             System.out.println("Exception: " + ex);        
         }
    }
}

But I always get the following output in the console:

rType is null

Why? And how to fix it?

Ksenia
  • 3,453
  • 7
  • 31
  • 63
  • Where is the `onNextButtonClick` method used? – fabian Mar 06 '18 at 09:39
  • @fabian, in RootScene.fxml there is button `` – Ksenia Mar 06 '18 at 09:43
  • @fabian, and this `onNextButtonClick` method is invoked and `nextButton` isn't null, but `rType` is null. – Ksenia Mar 06 '18 at 09:43
  • And where is `rType` in `RootScene.fxml`? `FXMLLoader` creates different controller instances for each fxml, at least the way you load them... – fabian Mar 06 '18 at 09:46
  • I defined one controller for the both fxml: RootScene.fxml and FirstWindow.fxml... Could not it work so? – Ksenia Mar 06 '18 at 09:48
  • You used the same value for the `fx:controller` attribute in both fxmls which tells `FXMLLoader` to use the same controller ***class***. This does not mean `FXMLLoader`s save the controllers ***instances*** somewhere to reuse them. Every time you load a fxml that contains a `fx:controller` attribute a new instance of the controller class is created whether the same class was used as controller class before or not. – fabian Mar 06 '18 at 09:52
  • @fabian, thank you a lot! I've changed my code by creating separate controllers for every fxml, and this addressed my problem. If you write your comment as answer, I'll accept it! – Ksenia Mar 06 '18 at 10:27
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – SedJ601 Mar 06 '18 at 15:34

0 Answers0