我想在我的应用程序中加载一个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,它们都可以正常工作,而对于其他的,我得到了这个例外:
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)我不明白为什么我会有这个例外。
谢谢。
编辑:添加包含示例:
我的父母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>发布于 2014-02-28 10:29:34
这是解决办法。
为加载程序添加以下行:
loader.setLocation(JfxUtils.class.getResource(fxml));很坏的虫子。
发布于 2014-02-21 14:36:59
加载fxml的正确语法是:
FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));NullPointerException的原因可能是因为我没有看到任何关于fxml变量的定义。另外,我也不确定.openStream()在您的末尾是否正确。
有关FXML的更多信息,请参见这里。
编辑
根据这些评论,大约有3个地方,我看到你可能会得到这个NPE。
Place #1)加载程序是空的,因为我们可以看到它显然不是,这不是一个问题。JfxUtils是一个不存在的项,但是基于错误,这也不是它。Place #3 getResource(fxml)返回null,因为它指向不存在的东西,因此返回null,并且不能在空对象上打开流。
这句话:异常中的at javafx.fxml.FXMLLoader.isCyclic(FXMLLoader.java:1868)非常突出,尽管我现在没有时间深入研究它。我的直觉告诉我一些奇怪的原因可能与此有关。
https://stackoverflow.com/questions/21937057
复制相似问题