0

I am trying to add elements into a tableView but this error occurs :

Caused by: java.lang.NullPointerException: Cannot invoke Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.tableView" is null

This is caused by this line : tableView.setItems(categorieList);

Here is the code :

public class MainClass {
    @FXML
    private BorderPane borderPane;
    @FXML
    private TextField ajouterCategorie_TEXTFIELD;
    @FXML
    private TextField ajouterLien_TEXTFIELD;
    @FXML
    private TextField comparaisonCategorie;

    @FXML
    private TableColumn<Categorie,String> tableColumn;

    @FXML
    private TableView<Categorie> tableView;


    private ObservableList<Categorie> categorieList = FXCollections.observableArrayList();

    //  Stage stage =(Stage) borderPane.getScene().getWindow(); // pour aller choper tout ce qu'il y a dans le borderpane

    public void pointDeDepart(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(RunMe.class.getResource("ecranDemarrage.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 650, 270);
        String path = "C:\\Users\\yacin\\OneDrive\\Desktop\\sources_classification java project\\src\\demonslayer.png";
        // pour mettre un logo
        File file = new File(path);
        String localUrl = file.toURI().toURL().toString();
        Image image = new Image(localUrl, false);
        stage.getIcons().add(image);
        // fin logo
        stage.setTitle("Classifieur");
        stage.setScene(scene);
      //  stage.setResizable(false);
        stage.show();
    }

    @FXML
    protected void ajouterCategorie() {
        changerInterface("ajouterCategorie");
    }
    @FXML
    protected void ajouterCategorie2() {
        String categorie = ajouterCategorie_TEXTFIELD.getText();
        Categorie categorieInstance = new Categorie(categorie);
        categorieList.add(categorieInstance);
        System.out.println("categorie ajoutee : " + categorieInstance.getNomCategorie());
    }
    @FXML
    protected void ajouterLien() {
        changerInterface("ajouterLien");
    }

    // lorsqu'on a appuye sur ajouterLien on doit appuyer encore sur ajouterLien2
    @FXML
    protected void ajouterLien2() {
        String lien = ajouterLien_TEXTFIELD.getText();
        System.out.println("lien ajoute : " + lien);
    }

    @FXML
    protected void afficherCategorie() {
        tableView.setItems(categorieList);
        changerInterface("afficherCategories");
    }

    private void changerInterface(String interfaceEntree) {
        Parent root = null;
        try {
          root = FXMLLoader.load(getClass().getResource(interfaceEntree + ".fxml"));

        } catch(IOException ex) {
            System.out.println("il y a une erreur dans 'changer interface'");
        }
        borderPane.setCenter(root);
    }

}

Here is the FXML:

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.demo.MainClass">
   <center>
      <TableView fx:id="tableView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn fx:id="tableColumn" prefWidth="598.6666564941406" text="C1" />
        </columns>
      </TableView>
   </center>
</BorderPane>

Here is the main FXML :

<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="278.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.demo.MainClass">
   <left>
      <VBox prefHeight="400.0" prefWidth="147.0" style="-fx-background-color: grey;" BorderPane.alignment="CENTER">
         <children>
            <Button mnemonicParsing="false" onAction="#ajouterCategorie" prefHeight="92.0" prefWidth="147.0" text="ajouter catégorie" />
            <Button mnemonicParsing="false" onAction="#ajouterLien" prefHeight="101.0" prefWidth="147.0" text="ajouter lien" />
            <Button mnemonicParsing="false" onAction="#afficherCategorie" prefHeight="104.0" prefWidth="147.0" text="afficher catégories" />
         </children>
      </VBox>
   </left>
</BorderPane>
James_D
  • 201,275
  • 16
  • 291
  • 322
  • 2
    There are no elements in the fxml, of course it won’t work. Is that all the fxml? And all the code? Is this supposed to be a [mcve] or is it deliberately incomplete? Could you make it a [mcve]? – jewelsea Jan 25 '22 at 12:46
  • Where is the `afficherCategorie()` method called from? It is annotated `@FXML`, but is not included as a handler method in your FXML file. – James_D Jan 25 '22 at 12:48
  • I edited my question. There is a main interface called "EcranDeDemarrage" who contains 3 buttons : "ajouterCategorie" linked with the method : "ajouterCategorie2" "ajouterLien" linked with "ajouterLien2" and "afficherCategorie" linked with "afficherCategorie" – IchigoKurosaki14 Jan 25 '22 at 13:00
  • 3
    Both fxml files reference the same controller class. Don’t do that. – jewelsea Jan 25 '22 at 13:02
  • 1
    `tableView` is only initialized in the controller that is created when you load the first FXML file you posted. The `afficherCategorie()` method is called on the controller that is created when you load the second FXML file you posted; `tableView` is not initialized in that controller. Hence the null pointer exception. – James_D Jan 25 '22 at 18:55

0 Answers0