填充ListView时,空指针异常

时间:2019-05-28 01:51:25

标签: java listview javafx

我正在为食谱创建应用程序,并试图将数据库中的食谱显示到ListView中。当我在ListView上调用setItems()时,我得到了一个空指针异常。我对JavaFX不太熟悉,所以在这里我不知道如何防止它。我确保ListView有一个fx:id,我的fxml文件指定了控制器,并且我尝试了其他一些解决此问题的方法。我的猜测是问题与我从MainMenu_Controller加载FXML,然后尝试从ViewRecipes_Controller将项目添加到ListView的事实有关,但我一直无法弄清楚。

主类:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/Cookbook_MainMenu.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
            primaryStage.setTitle("Cookbook");
            primaryStage.setScene(scene);
            primaryStage.show();
            DatabaseConnection db = new DatabaseConnection();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

MainMenu_Controller:

public class MainMenu_Controller {

    //controls when user clicks view recipes
    @FXML
    protected void viewRecipesBtnClick(ActionEvent event){
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/Cookbook_ViewRecipes.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
            Stage primaryStage = new Stage();
            primaryStage.setTitle("View Recipes");
            primaryStage.setScene(scene);
            primaryStage.show();
            //close previous window
            ((Node)(event.getSource())).getScene().getWindow().hide();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    //controls when user clicks add recipe
    @FXML
    protected void addRecipeBtnClick(ActionEvent event){
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/Cookbook_MainMenu.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
            Stage primaryStage = new Stage();
            primaryStage.setTitle("Add Recipe");
            primaryStage.setScene(scene);
            primaryStage.show();
            //close previous window
            ((Node)(event.getSource())).getScene().getWindow().hide();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

ViewRecipes_Controller:

public class ViewRecipes_Controller {

    @FXML
    private ListView recipeListView;

    public ViewRecipes_Controller() {
        DatabaseConnection db = new DatabaseConnection();
        ObservableList<String> names = FXCollections.observableArrayList();
        names = db.getRecipeName();
        recipeListView.setItems(names);     //error occurs here
    }

    //controls when user clicks back to menu
    @FXML
    protected void backToMenuBtnClick(ActionEvent event){
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/Cookbook_MainMenu.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
            Stage primaryStage = new Stage();
            primaryStage.setTitle("Cookbook");
            primaryStage.setScene(scene);
            primaryStage.show();
            //close previous window
            ((Node)(event.getSource())).getScene().getWindow().hide();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Cookbook_MainMenu.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="261.0" prefWidth="600.0" stylesheets="@application/styles.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainMenu_Controller">
   <children>
      <Label layoutX="173.0" layoutY="20.0" styleClass="titleLbl" text="Cookbook">
         <font>
            <Font name="Ink Free" size="68.0" />
         </font>
      </Label>
      <Button fx:id="viewRecipeBtn" layoutX="136.0" layoutY="188.0" mnemonicParsing="false" onAction="#viewRecipesBtnClick" styleClass="btnDefault" text="View Recipes">
         <font>
            <Font name="ELEGANCE" size="12.0" />
         </font>
      </Button>
      <Button fx:id="addRecipeBtn" layoutX="380.0" layoutY="188.0" mnemonicParsing="false" onAction="#addRecipeBtnClick" styleClass="btnDefault" text="Add Recipe" />
   </children>
</AnchorPane>

Cookbook_ViewRecipes.fxml:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="416.0" prefWidth="301.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ViewRecipes_Controller">
   <children>
      <Label layoutX="54.0" styleClass="titleLbl" text="Recipes">
         <font>
            <Font name="Ink Free" size="62.0" />
         </font>
      </Label>
      <Button fx:id="backToMenuBtn" layoutX="51.0" layoutY="355.0" mnemonicParsing="false" onAction="#backToMenuBtnClick" styleClass="btnDefault" text="Back to Menu" />
      <ListView fx:id="recipeListView" layoutX="51.0" layoutY="116.0" prefHeight="200.0" prefWidth="200.0" styleClass="recipeList" />
      <Button fx:id="getDetailsBtn" layoutX="177.0" layoutY="355.0" mnemonicParsing="false" styleClass="btnDefault" text="Get Details" />
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:-1)

我能为您提供的最佳解决方案是,在不进一步了解此处发生的情况的情况下,请确保在继续访问该对象之前,recipeListView不为null:

public ViewRecipes_Controller() {
    DatabaseConnection db = new DatabaseConnection();
    ObservableList<String> names = FXCollections.observableArrayList();
    names = db.getRecipeName();
    if (recipeListView != null) {
        recipeListView.setItems(names);     //error occurs here
    }
    else {
        System.out.println("Unable to set items, recipeListView is null")
    }   
}

如果您想找出为什么对象是null的原因,则应该运行带有断点的调试会话并分析代码流。