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.