0

In FXML I have root BorderPane, in the center of that is another BorderPane and in center of that second Borderpane I have GridPane. I want to add TextFields depending on the size of the array. I don't know how to access that GridPane. In FXML I've set fx:id, but in my controller it's not visible.(@FXML GridPane nameInFxml)

    <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="466.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:id="rootBorderPane">
   <left>
      <TableView fx:id="repairsTable" prefHeight="372.0" prefWidth="151.0" BorderPane.alignment="CENTER">
         <columns>
             <TableColumn fx:id="data" prefWidth="55.0" text="Data">
                 <cellValueFactory>
                     <PropertyValueFactory property="data" />
                 </cellValueFactory>
             </TableColumn>
             <TableColumn fx:id="mechanik" prefWidth="41.0" text="Mechanik">
                 <cellValueFactory>
                     <PropertyValueFactory property="mechanik" />
                 </cellValueFactory>
             </TableColumn>
         </columns></TableView>
   </left>
   <center>
      <BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" fx:id="borderPane">
         <center>
            <GridPane alignment="CENTER" hgap="5.0" prefHeight="300.0" prefWidth="448.0" BorderPane.alignment="CENTER" fx:id="details" >
              <columnConstraints>
                <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="220.0" minWidth="10.0" prefWidth="71.0" />
                <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="346.0" minWidth="10.0" prefWidth="244.0" />
                  <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="346.0" minWidth="10.0" prefWidth="66.0" />
                  <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" maxWidth="346.0" minWidth="10.0" prefWidth="80.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints maxHeight="95.0" minHeight="0.0" prefHeight="25.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="200.0" minHeight="0.0" prefHeight="23.0" vgrow="SOMETIMES" />
                <RowConstraints maxHeight="290.0" minHeight="10.0" prefHeight="290.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>

                  <Label fx:id="coByloRobioneLabel" text="Co było robione" GridPane.columnIndex="0" />
                  <Label fx:id="nettoLabel" text="Netto" GridPane.columnIndex="01" />
                  <Label fx:id="bruttoLabel" text="Brutto" GridPane.columnIndex="2" />
               </children>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </GridPane>
         </center>
         <top>
            <GridPane prefHeight="51.0" prefWidth="449.0" BorderPane.alignment="CENTER">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label fx:id="mechanikLabel" text="Mechanik   " GridPane.columnIndex="0" />
                  <Label text="Data" GridPane.columnIndex="2" />
                  <Label text="Nr Rej." GridPane.columnIndex="4" />
                  <TextField fx:id="mechanikTextField" GridPane.columnIndex="1" />
                  <TextField fx:id="dataTextField" GridPane.columnIndex="3" />
                  <TextField fx:id="nrRejTextField" GridPane.columnIndex="5" />
               </children>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </GridPane>
         </top>
      </BorderPane>
   </center>
   <bottom>
      <HBox alignment="CENTER" prefHeight="44.0" prefWidth="600.0" spacing="20" BorderPane.alignment="CENTER">
         <children>
            <TextField fx:id="searchTextField" />
            <Button fx:id="nowyButton" mnemonicParsing="false" text="Nowy" />
            <Button fx:id="zapiszButton" mnemonicParsing="false" text="Zapisz" />
            <Button fx:id="usunButton" mnemonicParsing="false" text="Usuń" />
            <Button fx:id="newRecord" mnemonicParsing="false" text="Nowa pozycja w naprawie" />
         </children>
      </HBox>
   </bottom>
</BorderPane>

I want to access that GridPane with fx:id = "details".

Controller:

public class RepairsController {
    @FXML
    TextField mechanikTextField;
    @FXML
    TextField dataTextField;
    @FXML
    TextField nrRejTextField;
    @FXML
    Button nowyButton;
    @FXML
    Button usunButton;
    @FXML
    Button zapiszButton;
    @FXML
    TextField searchTextField;
    @FXML
    TableView<Repair> repairsTable;
    @FXML
    GridPane details;





    private ObservableList<Repair> repairs;
    private Car car;

    public RepairsController() {

    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public void initialize(){

        repairs = DataSource.getInstance().queryAllRepairs(car.getNrRej());

        filter(repairs);
        onChangeListener();
        repairsTable.getSelectionModel().selectFirst();

        nowyButtonPressed();
        usunButtonPressed();


    }

    @FXML
    public void filter(ObservableList<Repair> list) {

        FilteredList<Repair> filteredData = new FilteredList<>(list, p -> true);

        searchTextField.textProperty().addListener((observable, oldValue, newValue) -> {

            filteredData.setPredicate(repair-> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }
                String loweCaseFilter = newValue.toLowerCase();
                if (repair.getData().toLowerCase().contains(loweCaseFilter)) {
                    return true;
                }

                if((repair.getMechanik().toLowerCase().contains(loweCaseFilter))){
                    return true;
                }

                return false;

            });

        });
        SortedList<Repair> sortedData = new SortedList<>(filteredData);
        sortedData.comparatorProperty().bind(repairsTable.comparatorProperty());
        repairsTable.setItems(sortedData);

    }

    private void onChangeListener(){
        repairsTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Repair>() {
            @Override
            public void changed(ObservableValue<? extends Repair> observableValue, Repair repair, Repair t1) {
                if(t1 != null) {
                    mechanikTextField.setText(repairsTable.getSelectionModel().getSelectedItem().getMechanik());
                    dataTextField.setText(repairsTable.getSelectionModel().getSelectedItem().getData());
                }



                TextField[] coTextField = new TextField[30];
                if(repairsTable.getSelectionModel().getSelectedItem().getRepairDetailsList() !=null) {

                    for (int i = 0; i < repairsTable.getSelectionModel().getSelectedItem().getRepairDetailsList().size(); i++) {
//at this point I get the exception
                       details.add(coTextField[i], 2, 2);

                    }
                }
            }
        });
    }

    public void nowyButtonPressed(){
        nowyButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Repair repair = new Repair();
                repairs.add(repair);
            }
        });
    }

    public void usunButtonPressed(){
        usunButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                repairs.remove(repairsTable.getSelectionModel().getSelectedItem());
            }
        });
    }




}
3
  • i don't think it's loading i think it's accessing that is the issue @Matt Commented Nov 18, 2019 at 19:53
  • I've matched the fx:id attribute to the field name, but I can't access that GridPane. In my controller GridPane is grayed, while it supposed to be purple, since it matches with FXML. @Matt Commented Nov 18, 2019 at 19:59
  • I commented that out before I changed field name from detailsGridPane to details. So the updated filed GridPane details isn't working neither. I get Exception in thread "JavaFX Application Thread" java.lang.NullPointerException. Commented Nov 18, 2019 at 20:54

1 Answer 1

2

You are recieving a Null Pointer do to this line

TextField[] coTextField = new TextField[30];

No Textfields in this array have been initialized you have just created an empty array of size 30 that can hold TextFields

If you put this code after that line(TextField[] coTextField = new TextField[30];) I assume your NPE will disappear

for (int i = 0; i < coTextField.length; i++) 
    coTextField[i] = new TextField();

This code will create a new Textfield for every index in your array

Let me know if you have any other questions about anything in this post

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.