Java 填充ListView时出现空指针异常
我正在为一本食谱创建一个应用程序,并试图将食谱从数据库显示到列表视图中。在ListView上调用setItems()时,我得到一个空指针异常。我对JavaFX不太熟悉,所以我不知道如何在这里防止它。我确保ListView有一个fx:id,我的fxml文件指定了控制器,并且我尝试了一些其他方法来解决其他人的问题。我的猜测是,问题与以下事实有关:我正在从MainMenu\u控制器加载FXML,然后尝试从ViewRecipes\u控制器将项目添加到ListView,但我一直无法找到答案 主要类别:Java 填充ListView时出现空指针异常,java,listview,javafx,Java,Listview,Javafx,我正在为一本食谱创建一个应用程序,并试图将食谱从数据库显示到列表视图中。在ListView上调用setItems()时,我得到一个空指针异常。我对JavaFX不太熟悉,所以我不知道如何在这里防止它。我确保ListView有一个fx:id,我的fxml文件指定了控制器,并且我尝试了一些其他方法来解决其他人的问题。我的猜测是,问题与以下事实有关:我正在从MainMenu\u控制器加载FXML,然后尝试从ViewRecipes\u控制器将项目添加到ListView,但我一直无法找到答案 主要类别: p
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);
}
}
主菜单\u控制器:
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();
}
}
}
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();
}
}
}
ViewRecipes\u控制器:
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();
}
}
}
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();
}
}
}
公共类ViewRecipes\u控制器{
@FXML
私有列表视图recipeListView;
公共视图配方\控制器(){
DatabaseConnection db=新建DatabaseConnection();
ObservableList name=FXCollections.observableArrayList();
name=db.getRecipeName();
recipeListView.setItems(名称);//此处发生错误
}
//控制用户单击返回菜单的时间
@FXML
受保护的void backtomenubtclick(ActionEvent事件){
试一试{
父根=fxmloader.load(getClass().getResource(“/Cookbook_MainMenu.fxml”);
场景=新场景(根);
scene.getStylesheets().add(getClass().getResource(“styles.css”).toExternalForm());
阶段primaryStage=新阶段();
初级阶段。设置标题(“食谱”);
初级阶段。场景(场景);
primaryStage.show();
//关闭上一窗口
((节点)(event.getSource()).getScene().getWindow().hide();
}捕获(例外e){
e、 printStackTrace();
}
}
}
Cookbook_main menu.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")
}
}
public ViewRecipes\u Controller(){
DatabaseConnection db=新建DatabaseConnection();
ObservableList name=FXCollections.observableArrayList();
name=db.getRecipeName();
if(recipeListView!=null){
recipeListView.setItems(名称);//此处发生错误
}
否则{
System.out.println(“无法设置项,recipeListView为空”)
}
}
如果要找出对象为null
的原因,则应运行带有断点的调试会话,并分析代码流。调用recipeListView.setItems(名称);在initialize()方法中。最好参考