NullPointerException 加载 fxml

2024-04-24

我想在我的应用程序中加载 fxml 文件。我使用下一个代码:

try {
   FXMLLoader loader = new FXMLLoader();
   loader.setController(null);
   loader.setRoot(null);
   loader.setResources(ResourceBundle.getBundle(BUNDLE));
   Node root = null;
   root = (Node) loader.load(JfxUtils.class.getResource(fxml).openStream());
   return root;
} catch (IOException e) {
   throw new IllegalStateException("cannot load FXML screen", e);
}

对于一些 fxml,一切正常,对于其他 fxml,我得到这个异常:

Caused by: java.lang.NullPointerException
    at javafx.fxml.FXMLLoader.equals(FXMLLoader.java:1856)
    at javafx.fxml.FXMLLoader.isCyclic(FXMLLoader.java:1868)
    at javafx.fxml.FXMLLoader.access$2100(FXMLLoader.java:71)
    at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:941)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:570)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2356)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2172)

我不明白为什么我会得到这个例外。

Thanks.

EDIT:添加包含示例:

我的父母 fxml

<fx:root type="javafx.scene.layout.AnchorPane" fx:id="scrollPane" id="scrollStocksList"
    xmlns:fx="http://javafx.com/fxml"
    fx:controller="net.StocksListRunningController">
    <fx:include fx:id="tableListStock"
        source="/fxml/stocksList.fxml" />
</fx:root>

我的包含文件:

<fx:root type="javafx.scene.layout.VBox" xmlns:fx="http://javafx.com/fxml"
    fx:controller="net.StockListTableController">
    <TableView fx:id="stocksList" onMouseClicked="#openDetail">
        <columns>
            <TableColumn text="Titre" prefWidth="125">
                <cellValueFactory>
                    <PropertyValueFactory property="title" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Quantité" prefWidth="75" fx:id="quantityColumn">
                <cellValueFactory>
                    <PropertyValueFactory property="quantity" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Prix unitaire" prefWidth="100">
                <cellValueFactory>
                    <PropertyValueFactory property="unitPrice" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Prix total" prefWidth="120">
                <cellValueFactory>
                    <PropertyValueFactory property="price" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Sens" prefWidth="50">
                <cellValueFactory>
                    <PropertyValueFactory property="direction" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Date d'achat" prefWidth="100">
                <cellValueFactory>
                    <PropertyValueFactory property="date" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Jours écoulés" prefWidth="100">
                <cellValueFactory>
                    <PropertyValueFactory property="dayRunning" />
                </cellValueFactory>
            </TableColumn>
        </columns>
    </TableView>
</fx:root>

这是解决方案。

为加载器添加这一行:

loader.setLocation(JfxUtils.class.getResource(fxml));

非常糟糕的错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NullPointerException 加载 fxml 的相关文章

随机推荐