我正在更新我的GUI的列表视图并发出以下错误:'线程中的异常" JavaFX应用程序线程"显示java.lang.NullPointerException'我这样做了:
// loading the existing notebooks
File file = new File(System.getProperty("user.home") +
"/Documents/Pappel");
String[] directories = file.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
int i = 0;
while (i < directories.length){
notebooksContainer.getItems().add(directories[i]);
i ++;
}
错误发生在&#39; notebooksContainer.getItems()。add(notebookTitle);&#39;。 notebooksContainer是一个listView,它使用字符串填充,也可以从不同的类继承。此外,notebookTitle不为null,因为它由print语句打印。有什么想法吗?
这是我在要扩展的类中填充视图列表的位置。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="PappelNotebookController">
<children>
<VBox layoutX="10.0" layoutY="51.0" prefHeight="512.0" prefWidth="137.0" spacing="5.0" AnchorPane.bottomAnchor="37.0" AnchorPane.topAnchor="51.0">
<children>
<HBox fx:id="notebooks" alignment="CENTER_LEFT" prefHeight="44.0" prefWidth="106.0" spacing="10.0">
<children>
<ImageView fitHeight="37.0" fitWidth="34.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@open-iconic-master/png/book-8x.png" />
</image>
</ImageView>
<Label text="Notebooks">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</HBox>
<HBox fx:id="notes" alignment="CENTER_LEFT" prefHeight="44.0" prefWidth="205.0" spacing="10.0">
<children>
<ImageView fitHeight="37.0" fitWidth="34.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@open-iconic-master/png/document-8x.png" />
</image>
</ImageView>
<Label text="Notes">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</HBox>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</VBox>
<Label layoutX="56.0" layoutY="24.0" text="Library" />
<TextArea fx:id="noteContent" layoutX="370.0" layoutY="80.0" prefHeight="512.0" prefWidth="622.0" promptText="Start writing!" AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="370.0" AnchorPane.rightAnchor="8.0" AnchorPane.topAnchor="80.0" />
<TextField fx:id="noteTitle" layoutX="370.0" layoutY="23.0" prefHeight="26.0" prefWidth="247.0" promptText="Title" AnchorPane.leftAnchor="370.0" AnchorPane.rightAnchor="383.0">
<font>
<Font size="25.0" />
</font>
</TextField>
<Label layoutX="215.0" layoutY="24.0" text="Notebooks" />
<HBox fx:id="addNotebook" alignment="CENTER" layoutX="147.0" layoutY="557.0" prefHeight="35.0" prefWidth="205.0" spacing="10.0" AnchorPane.bottomAnchor="8.0">
<children>
<ImageView fitHeight="26.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@open-iconic-master/png/plus-8x.png" />
</image>
</ImageView>
<Label text=" New notebook">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</HBox>
<ListView fx:id="notebooksContainer" layoutX="158.0" layoutY="49.0" prefHeight="503.0" prefWidth="185.0" AnchorPane.bottomAnchor="48.0" AnchorPane.topAnchor="49.0" />
</children>
</AnchorPane>
这是我的fxml:
cont.push