Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've two Controllers

1. `FXMLDocumentController` has `TableView` CustomerTable.
2. `NewCustomerController` a second Controller to add a new row.

I can pass values from First Controller to Second Controller.
But I'm not able revert it from Second Controller to First Controller.


What I have tried:

FXMLDocument.fxml

XML
<AnchorPane id="AnchorPane" prefHeight="283.0" prefWidth="437.0"     xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1"fx:controller="mytableview.FXMLDocumentController">
        <children>
            <Button fx:id="button" layoutX="309.0" layoutY="25.0" onAction="#handleButtonAction" text="New Customer" />
          <TableView fx:id="customerTable" layoutX="6.0" layoutY="61.0" prefHeight="215.0" prefWidth="426.0">
            <columns>
              <TableColumn fx:id="custname" prefWidth="75.0" text="Customer Name" />
              <TableColumn fx:id="city" prefWidth="75.0" text="City" />
            </columns>
             <columnResizePolicy>
                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
             </columnResizePolicy>
          </TableView>
        </children>
    </AnchorPane>



NewCustomer.fxml

XML
<AnchorPane id="AnchorPane" prefHeight="172.0" prefWidth="209.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="com.newcustomer.NewCustomerController">
    <children> 
<Button layoutX="141.0" layoutY="129.0" mnemonicParsing="false" onAction="#newCustomer" text="Add" />
        <TextField fx:id="cNameTextField" layoutX="14.0" layoutY="26.0" promptText="Customer Name" />
   <pre><pre lang="xml"> <TextField fx:id="custCityTextField" layoutX="14.0" layoutY="77.0" promptText="City" />
    </children>
</AnchorPane>




FXMLDocumentController.java

Java
public class FXMLDocumentController implements Initializable {

   @FXML
   public TableView<Customer> customerTable;

   @FXML
   public TableColumn<Customer, String> custname;

   @FXML
   public TableColumn<Customer, String> city;

   @Override
   public void initialize(URL url, ResourceBundle rb) {
   }

   @FXML
   private void handleButtonAction(ActionEvent event) throws IOException {
       Parent parent = FXMLLoader.load(getClass().getResource("/com/newcustomer/NewCustomer.fxml"));
       Stage stage = new Stage();
       Scene scene = new Scene(parent);
       stage.setScene(scene);
       stage.show();
   }

   public void inflateUI(Customer customer) {
       custname.setCellValueFactory(new PropertyValueFactory<>("name"));
       city.setCellValueFactory(new PropertyValueFactory<>("city"));
       customerTable.getItems().add(customer);
   }


NewCustomerController.java

Java
public class NewCustomerController implements Initializable {

   @FXML
   private TextField cNameTextField;

   @FXML
   private TextField custCityTextField;

   @Override
   public void initialize(URL url, ResourceBundle rb) {
   }

   public void newCustomer(ActionEvent e) throws IOException {
       String name = cNameTextField.getText();
       String stringCity = custCityTextField.getText();

       Customer customer = new Customer(10, name, stringCity);
       FXMLLoader fXMLLoader = new FXMLLoader(getClass().getResource("/mytableview/FXMLDocument.fxml"));
       FXMLDocumentController fXMLDocumentController = fXMLLoader.<FXMLDocumentController>getController();
       fXMLDocumentController.inflateUI(customer); // Getting NPE at this line.
       }

   }


MyTableView.java

Java
public class MyTableView extends Application {
    
        public MyTableView() {
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    
            Scene scene = new Scene(root);
    
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    }


I'm getting NPE here at this line
fXMLDocumentController.inflateUI(customer); // Getting NPE at this line.

In NewCustomerController.java.

Here is a source code Uploadfiles.io - MyTableView.zip[^]
Posted
Updated 4-Oct-19 8:15am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900