提问人:IchigoKurosaki14 提问时间:1/25/2022 最后编辑:James_DIchigoKurosaki14 更新时间:1/26/2022 访问量:2932
NullPointerException:无法调用“javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)” ||JavaFx [重复]
NullPointerException : Cannot invoke "javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)" || JavaFx [duplicate]
问:
我正在尝试将元素添加到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>
答: 暂无答案
评论
afficherCategorie()
@FXML
tableView
仅在加载发布的第一个 FXML 文件时创建的控制器中初始化。该方法在加载发布的第二个 FXML 文件时创建的控制器上调用; 未在该控制器中初始化。因此,空指针异常。afficherCategorie()
tableView