NullPointerException load fxml

拟墨画扇 提交于 2019-12-02 17:59:57

问题


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);
}

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.

Thanks.

EDIT: Add include exemple:

My parent 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>

My include file:

<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>

回答1:


Here is the solution.

Add this line for the loader:

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

Very bad bug.




回答2:


The proper syntax for loading fxml is:

FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));

The reason for your NullPointerException is likely due to the fact that I don't see any definition of your fxml variable. Also, I'm not sure about that .openStream() bit at the end of yours.

see here for more info on FXML.

EDIT

In light of the comments, There are..3 or so places there I see that you could be getting this NPE.

Place #1) loader is null, since we can see it's clearly not, this is a non-issue. place #2) JfxUtils is a non-existing item, but based on the error, this is not it either. Place #3 the getResource(fxml) is returning the null because it either points to something that doesn't exist, thus returning null, and you can't open a stream on a null object.

this line: at javafx.fxml.FXMLLoader.isCyclic(FXMLLoader.java:1868) in the exception stands out to me, although I don't have time right this second to dive in and explore it. Something in my gut tells me the cause of some of the weirdness might be tied into it.



来源:https://stackoverflow.com/questions/21937057/nullpointerexception-load-fxml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!