我正在为食谱创建一个应用程序,我正在尝试将数据库中的食谱显示到 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>
这里发生的事情的情况下,我可以为您提供的最佳解决方案是在继续访问对象之前确保recipeListView
不为空:
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
对象的原因,则应运行带有断点的调试会话并分析代码流。