This discussion is archived
4 Replies Latest reply: Dec 3, 2012 12:28 PM by edward17 RSS

Button not initialized

964914 Newbie
Currently Being Moderated
I have a controller class which extends a simple java class. Now inside the base class I declare a button whose name matches the fx:id of the button in the fxml file. Now when the fxml is loaded the controller is initialized. however, the button is not initialized and is still null. I presume the button should have been initialized by itself.

Sample code:
public class TopClass {
    public TopClass () {
        super();
    }

   @FXML
    private Button button1;
    @FXML
    private Button button2;
}

public class MyController extends TopClass implements Initializable {

@Override
    public void initialize(URL url, ResourceBundle rb) {
     // do some initlization work
   button1.setDisable(true); // It throws null pointer here.
    }
}

*FXML:*

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MyController">
  <children>
    
    <Button id="button1" fx:id="button1" layoutX="500.0" layoutY="12.0"  prefHeight="21.0" prefWidth="100.0" text="button 1" />
    <Button id="button2" fx:id="button2" layoutX="400.0" layoutY="12.0"  prefHeight="21.0" prefWidth="100.0" text="button 2" />
    
    
  </children>
</AnchorPane>
Any idea why it's not initialized. If the button is in MyController class, then buttons are initialized.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points