0

尝试从不同的包访问 .fxml 时遇到问题。假设我的 login.fxml 在客户包中。登录成功后会跳转到首页。在主页上,我有一个指向购物车包中的 myShoppingCart.fxml 的按钮 tat 链接。但是,执行此操作时出现空指针异常错误:

public void goToCart(ActionEvent event) {
    String userName = CustomerLoginController.userLoggedIn.getName();
    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        Pane p = (Pane) fxmlLoader.load(getClass().getResource("cartCustHome.fxml").openStream());
        CartCustHomeUI fooController = (CartCustHomeUI) fxmlLoader.getController();
        fooController.retrieveUserName(userName);
        Stage stage = new Stage();
        Scene scene = new Scene(p);
        scene.getStylesheets().add(MainFrame.class.getResource("cart.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
        ((Node) (event.getSource())).getScene().getWindow().hide();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

尽管我已经导入了我的购物车包,但它没有得到我的 cartCustHome.fxml。我想知道为什么会这样。提前致谢。

4

1 回答 1

0

尝试

getClass().getResource("../customer/cartCustHome.fxml").openStream()

有关更多信息,请在网站上搜索“java getresource 返回 null”。关于这个主题有很多讨论,它们提供了全面的解释。

于 2013-07-31T08:59:18.377 回答