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>