0

I'm new to FXML and can't figure out how to get entered text from a TextField. Below is my FMXL file from scenebuilder and my controller

FXML:

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="392.0" prefWidth="559.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="validator.FXMLDocumentController">
    <children>
      <GridPane layoutX="6.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="6.0" AnchorPane.rightAnchor="-6.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="181.0" minWidth="10.0" prefWidth="132.0" />
          <ColumnConstraints hgrow="SOMETIMES" maxWidth="381.0" minWidth="10.0" prefWidth="348.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="138.0" minWidth="10.0" prefWidth="58.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
              <Button fx:id="button" onAction="#handleButtonAction" text="Validate" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <TextField id="XMLPath" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField id="XSDPath" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField id="OutputPath" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Xml Validation Tool" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="XML File Location:" GridPane.rowIndex="1" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="XSD File Location:" GridPane.rowIndex="2" />
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Output File Location:" GridPane.rowIndex="3" />
         </children>
      </GridPane>
    </children>
</AnchorPane>

Controller:

public class FXMLDocumentController{

    @FXML
    private TextField XMLPath;

    @FXML
    private TextField XSDPath;

    @FXML
    private TextField OutputPath;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println(XMLPath.getText());
        System.out.println(XSDPath.getText());
        System.out.println(OutputPath.getText());
    }

}

When I click the button to retrieve the text, it gives me a null pointer errors. I feel like I'm missing something simple.

Robert Petty
  • 197
  • 1
  • 3
  • 17
  • I think you need to implement an `initializable` to your controller so that it can initialize your annotated nodes – Bo Halim Dec 28 '16 at 18:02
  • How are you initializing the controller? – purring pigeon Dec 28 '16 at 18:02
  • 1
    @BoHalim Controllers have not needed to implement `Initializable` since version 2.1. – James_D Dec 28 '16 at 18:04
  • @James_D, Thanks for the information I thought we always had to initialize our controller ! :) – Bo Halim Dec 28 '16 at 18:06
  • 2
    @BoHalim Well, you usually do need to perform some initialization, but all you need is a method called `initialize()`; you don't need to implement the interface. And that is completely independent of the `@FXML`-annotated fields being injected. – James_D Dec 28 '16 at 18:08
  • @James_D Thanks pointing to other post. Didn't realize was using the wrong id tag. – Robert Petty Dec 28 '16 at 18:11

0 Answers0