NullPointerException加载fxml [英] NullPointerException load fxml

查看:69
本文介绍了NullPointerException加载fxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to load an fxml file in my application. I use the next code:

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可以得到此异常:

With some fxml, all work fine, with others I get this exception:

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)

我不明白为什么会出现这种异常.

I don't understand why I get this exception.

谢谢.

添加包含示例:

我的父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>

推荐答案

这是解决方案.

为加载程序添加以下行:

Add this line for the loader:

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

非常严重的错误.

这篇关于NullPointerException加载fxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆