NullPointerException:无法调用“javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)” ||JavaFx [重复]

NullPointerException : Cannot invoke "javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)" || JavaFx [duplicate]

提问人:IchigoKurosaki14 提问时间:1/25/2022 最后编辑:James_DIchigoKurosaki14 更新时间:1/26/2022 访问量:2932

问:

我正在尝试将元素添加到tableView中,但发生了此错误:

原因:java.lang.NullPointerException:无法调用 原因:java.lang.NullPointerException:无法调用“javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)”,因为“this.tableView”为空

这是由以下行引起的:tableView.setItems(categorieList);

代码如下:

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);
    }

}

下面是 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>

这是主要的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>
JavaFX NullPointerException 表视图

评论

2赞 jewelsea 1/25/2022
fxml中没有元素,当然它不起作用。这就是全部fxml吗?还有所有的代码?这应该是一个最小的可重现示例,还是故意不完整?你能把它做成一个最小的可重复的例子吗?
0赞 James_D 1/25/2022
从哪里调用该方法?它被注释,但不作为处理程序方法包含在FXML文件中。afficherCategorie()@FXML
0赞 IchigoKurosaki14 1/25/2022
我编辑了我的问题。有一个名为“EcranDeDemarrage”的主界面,其中包含 3 个按钮:“ajouterCategorie”与方法链接:“ajouterCategorie2” “ajouterLien”与“ajouterLien2”链接,“afficherCategorie”与“afficherCategorie”链接
3赞 jewelsea 1/25/2022
两个 fxml 文件都引用相同的控制器类。别这样。
1赞 James_D 1/26/2022
tableView仅在加载发布的第一个 FXML 文件时创建的控制器中初始化。该方法在加载发布的第二个 FXML 文件时创建的控制器上调用; 未在该控制器中初始化。因此,空指针异常。afficherCategorie()tableView

答: 暂无答案