我在使用scenebuilder的javafx项目中继续使用Nullpointer异常。我通过&#34; opgaveLbl&#34;,&#34; feedbackLbl&#34;得到nullpointer。和&#34; groepsbewerkingListView&#34;。此外,当我尝试制作新对象groepsbewerkingenListView = new ListView<String>();
时,我的列表视图并未填满我的项目。
控制器
public class OefeningToevoegenSchermController extends AnchorPane {
private DomeinController controller;
private Opgave opgave;
private Feedback feedback;
private List<Groepsbewerking> groepsbewerkingen;
@FXML
private TextField OefeningNaam;
@FXML
private TextField Antwoord;
@FXML
private Button UploadOefening;
@FXML
private Button UploadFeedback;
@FXML
private AnchorPane AnchorPane;
@FXML
private TextField operator;
@FXML
private TextField operand;
@FXML
private ListView<String> groepsbewerkingenListView;
@FXML
private Label opgaveLbl;
@FXML
private Label feedbackLbl;
@FXML
private ListView<String> OefGroepsbewerkingenListView;
public OefeningToevoegenSchermController(DomeinController controller) {
this.controller = controller;
groepsbewerkingen = new ArrayList<>();
opgaveLbl.setVisible(false);
feedbackLbl.setVisible(false);
System.out.println(controller.geefGroepsBewerkingen());
groepsbewerkingenListView.setItems(controller.geefGroepsBewerkingen());
FXMLLoader loader = new FXMLLoader(getClass().getResource("OefeningToevoegenScherm.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
FXML文件 该文件与我的控制器文件位于同一个包中。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<fx:root id="AnchorPane" prefHeight="518.0" prefWidth="604.0" type="AnchorPane" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER" layoutX="223.0" layoutY="25.0" text="Nieuwe Oefening">
<font>
<Font size="20.0" />
</font>
</Label>
<TextField id="OefeningNaam" fx:id="OefeningNaam" layoutX="140.0" layoutY="104.0" prefWidth="196.0" />
<Label layoutX="140.0" layoutY="87.0" text="Naam (duidelijke omschrijving)" />
<TextField id="Antwoord" fx:id="Antwoord" layoutX="140.0" layoutY="272.0" prefWidth="196.0" />
<Label layoutX="140.0" layoutY="255.0" text="Antwoord" />
<Button id="UploadOefening" fx:id="UploadOefening" layoutX="141.0" layoutY="159.0" mnemonicParsing="false" onAction="#UploadOefening" text="upload" />
<Label layoutX="141.0" layoutY="142.0" text="PDF toevoegen met opgave" />
<Button id="UploadFeedback" fx:id="UploadFeedback" layoutX="141.0" layoutY="213.0" mnemonicParsing="false" onAction="#UploadFeedback" text="upload" />
<Label layoutX="141.0" layoutY="196.0" text="opgave" />
<Label layoutX="143.0" layoutY="307.0" text="Groepsbewerkingen" />
<ListView fx:id="OefGroepsbewerkingenListView" layoutX="140.0" layoutY="330.0" prefHeight="109.0" prefWidth="164.0" />
<Button layoutX="35.0" layoutY="427.0" mnemonicParsing="false" onAction="#GroepsbewerkingToevoegen" prefHeight="25.0" prefWidth="89.0" text="Add" />
<TextField fx:id="operator" layoutX="30.0" layoutY="330.0" prefHeight="25.0" prefWidth="89.0" />
<Label layoutX="35.0" layoutY="307.0" text="Operator" />
<TextField fx:id="operand" layoutX="30.0" layoutY="390.0" prefHeight="25.0" prefWidth="89.0" />
<Label layoutX="35.0" layoutY="367.0" text="Operand" />
<Button layoutX="237.0" layoutY="460.0" mnemonicParsing="false" onAction="#MaakOefening" prefHeight="25.0" prefWidth="89.0" text="Opslaan" />
<Label fx:id="opgaveLbl" layoutX="143.0" layoutY="163.0" prefHeight="17.0" prefWidth="196.0" />
<Label fx:id="feedbackLbl" layoutX="141.0" layoutY="217.0" prefHeight="17.0" prefWidth="203.0" />
<ListView fx:id="groepsbewerkingenListView" layoutX="377.0" layoutY="330.0" prefHeight="109.0" prefWidth="164.0" />
</children>
</fx:root>
答案 0 :(得分:3)
在调用@FXML
期间初始化任何load()
个注入的字段;所以在加载FXML之前它们是null。只需在构造函数中重新排序代码:
public OefeningToevoegenSchermController(DomeinController controller) {
this.controller = controller;
groepsbewerkingen = new ArrayList<>();
FXMLLoader loader = new FXMLLoader(getClass().getResource("OefeningToevoegenScherm.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
opgaveLbl.setVisible(false);
feedbackLbl.setVisible(false);
System.out.println(controller.geefGroepsBewerkingen());
groepsbewerkingenListView.setItems(controller.geefGroepsBewerkingen());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}