0

So im having a hard time trying to add items to a list view that i added using scenebuilder. i set the control to the fxml to the class im trying to use but im trying to populate the list view in the main class but it keeps giving null pointer exceptions below is the main class

import javafx.stage.Stage;

import java.io.IOException;

/**
 * JavaFX App
 */
public class App extends Application {
    @FXML Listview list;
    private static Scene scene;

    @Override
    public void start(Stage stage) throws IOException {
        SecondaryController control = new SecondaryController();
        control.fill();
        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }

    public static void main(String[] args) {
        launch();
    }

}

this is the class thats connected to the fxml file that has the list view

package org.example;

import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;

public class SecondaryController {
    public SecondaryController()
    {

    }
    @FXML private ListView list;

    public void fill()
    {
        list.getItems().addAll("red","blue","orange");

    }
    @FXML
    private void switchToDelete() throws IOException {

        App.setRoot("Delete");
    }
    @FXML
    private void switchToAdd() throws IOException {

        App.setRoot("Add");
    }

}

and this is the fxml file which holds the listview inside

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.SecondaryController">
   <children>
      <ListView fx:id="list" layoutX="91.0" layoutY="79.0" prefHeight="200.0" prefWidth="348.0" />
      <Button layoutX="30.0" layoutY="352.0" mnemonicParsing="false" onAction="#switchToDelete" text="Delete" />
      <Button layoutX="139.0" layoutY="352.0" mnemonicParsing="false" onAction="#switchToAdd" text="Add" />
   </children>
</AnchorPane>

I feel like im populating it correctly so im not understanding why it keeps giving a null pointer exception.

mipa
  • 10,369
  • 2
  • 16
  • 35
tcampos19
  • 11
  • 2
  • 1
    don't mix app and controller in one class - you'll end up with two instances, one with/out the fields injected (that's what throwing here) And static scope is the wrong approach, nearly always (and certainly here) – kleopatra Aug 29 '20 at 09:18
  • 1
    Does this answer your question? [Javafx - Can application class be the controller class](https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class) – kleopatra Aug 29 '20 at 09:23
  • 1
    As well as the previous comment, you need to call `fill()` on the actual controller (the one created for you when you call `fxmlLoader.load()`). You’re calling it on an arbitrary object you created which just happens to be the same class as the controller. The `@FXML`-annotated fields in that object won’t be initialized (how would they be - they have no connection at all to the FXML file). Is there a reason you’re not calling `fill()` from the controller’s `initialize()` method? – James_D Aug 29 '20 at 14:35

0 Answers0