JavaFX中FXMLLoader递归加载致空指针异常求助
RogerBook JavaFX应用:FXMLLoader递归加载与空指针异常问题
我正在开发大学作业项目——一款名为RogerBook的JavaFX社交应用,当前遇到FXMLLoader递归加载导致LoadException,以及后续PostView类实例变量的空指针异常问题。
PostView类继承自AnchorPane,既是帖子展示组件,也是对应FXML文件的控制器。每次执行new PostView();时都会触发递归加载错误,错误栈如下:
at javafx.fxml@21/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2722) at javafx.fxml@21/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:946) at javafx.fxml@21/javafx.fxml.FXMLLoader$RootElement.processAttribute(FXMLLoader.java:1302) at javafx.fxml@21/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230) at javafx.fxml@21/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:757) at javafx.fxml@21/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2853) at javafx.fxml@21/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2649) at javafx.fxml@21/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563) at javafx.fxml@21/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531) at com.rogerbook.ui/com.rogerbook.ui.PostView.<init>(PostView.java:31)
错误发生后,PostView类中所有实例变量都会抛出空指针异常,相关代码如下:
PostView类(省略导入语句)
package com.rogerbook.ui; public class PostView extends AnchorPane { PicPost post; @FXML private ImageView imageView = new ImageView(); @FXML private Label Username = new Label(); @FXML private Label Title = new Label(); public PostView(PicPost post){ this(); this.post = post; initializePostView(); } public PostView() { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Post.fxml")); fxmlLoader.setRoot(this); try{ fxmlLoader.load(); }catch(IOException e){ e.printStackTrace(); } } private void initializePostView() { if (post != null) { imageView.setImage(post.getImage()); Username.setText(post.getAuthor().getProfile().getUserName()); Title.setText(post.getTitle()); } } }
主启动类
public class HelloApplication extends Application { @Override public void start(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Hello!"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(); } }
注册页面控制器(HelloController)
public class HelloController { private Stage stage; private Scene scene; private Parent root; @FXML private TextField username; @FXML public TextField password; @FXML public TextField name; @FXML private TextField password2; @FXML private Label errmsg; @FXML public void switchToHome(ActionEvent event) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("scenefeedpage.fxml")); stage = (Stage)((Node)event.getSource()).getScene().getWindow(); root = fxmlLoader.load(); scene = new Scene(root); stage.setScene(scene); stage.show(); } @FXML public void onSignup(ActionEvent event) throws IOException { System.out.println(password.getText()); if(password.getText().isEmpty() || username.getText().isEmpty() || name.getText().isEmpty() || password2.getText().isEmpty()){ errmsg.setText("Please fill all the fields"); return; } if(password.getText().equals(password2.getText())){ if(Roger.signup(username.getText().toLowerCase().strip(), password.getText())) { switchToHome(event); } else { errmsg.setText("Incorrect Password, or user does not exist"); } } else{ errmsg.setText("Passwords do not match"); } } }
Feed页面控制器(FeedController)
public class FeedController implements Initializable { private Stage stage; private Scene scene; private Parent root; Post post; @FXML ImageView imageView; @FXML VBox view; @Override public void initialize(URL location, ResourceBundle resources) { System.out.println("Initializing Feed"); ArrayList<Post> feed = Roger.getFeed(); System.out.println("Feed done"); feed.add(new PicPost("Hi", new Image("com/RogerBook/ui/R.png"), new User("maro", "aa"))); //PostView postView1 = new PostView((PicPost)feed.get(0)); post = feed.get(0); view.getChildren().add(new PostView((PicPost)post)); //view.getChildren().add(new PostView()); } public void ViewPost(ActionEvent event) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("Post.fxml")); stage = (Stage)((Node)event.getSource()).getScene().getWindow(); root = fxmlLoader.load(); scene = new Scene(root); stage.setScene(scene); stage.show(); } }
应用流程:启动后进入注册页面,注册完成跳转至Feed页面,FeedController向VBox中添加PostView组件时触发上述问题。已排查数小时仍未解决,寻求帮助。
内容的提问来源于stack exchange,提问作者TheMaro
相关产品推荐
相关产品推荐

