0

I keep having Nullpointer exceptions in my javafx project with scenebuilder. I get the nullpointer by "opgaveLbl", "feedbackLbl" and "groepsbewerkingListView". Moreover, when I try to make a new object groepsbewerkingenListView = new ListView<String>(); My listview isn't getting filled with my items.

Controller

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 file This file is in the same package of my controller file.

<?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>
Stan Fieuws
  • 162
  • 1
  • 2
  • 13
  • 6
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Turing85 Apr 19 '18 at 16:52
  • @Turing85 Not really. The OP knows which fields are null, and none of the answers to that question address fields whose values are injected by frameworks being null when not expected to be. – James_D Apr 19 '18 at 16:53
  • I've looked in loads of probably duplicated questions, haven't found a single solution though :/ – Stan Fieuws Apr 19 '18 at 16:56
  • 1
    Why are people voting to close this? The suggested duplicate says *nothing* at all about `@FXML`-injected fields. Specifically, here the `fx:id`s all match the field names in the controller: the only problem is the order of execution of the method calls (and is not particularly obvious). How does the linked question answer this one? – James_D Apr 19 '18 at 17:44

1 Answers1

3

Any @FXML-injected fields are initialized during the call to load(); so they are null before you load the FXML. Just re-order the code in your constructor:

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

}
James_D
  • 201,275
  • 16
  • 291
  • 322