Java NullPointerException加载fxml
我想在我的应用程序中加载一个fxml文件。我使用下一个代码:Java NullPointerException加载fxml,java,javafx-2,fxml,Java,Javafx 2,Fxml,我想在我的应用程序中加载一个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
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)
我不明白为什么我会得到这个例外
谢谢
编辑:添加包含示例:
我的父母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>
加载fxml的正确语法是:
FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));
出现NullPointerException的原因可能是因为我没有看到fxml
变量的任何定义。另外,我不确定你最后的.openStream()位
有关FXML的更多信息,请参阅
编辑
根据评论,有…3个左右的地方我看到你可以得到这个NPE
Place#1)loader为空,因为我们可以看到它显然不是空的,这不是问题。
place#2)JfxUtils是一个不存在的项,但根据错误,它也不是。
Place#3getResource(fxml)返回null,因为它指向不存在的东西,因此返回null,并且不能在null对象上打开流
异常中的这一行:位于javafx.fxml.fxmloader.isCyclic(fxmloader.java:1868)
对我来说非常突出,尽管我现在没有时间深入研究它。我的直觉告诉我一些奇怪的原因可能与此有关。以下是解决办法
为加载器添加此行:
loader.setLocation(JfxUtils.class.getResource(fxml));
非常糟糕的错误。似乎对象
fxml
为空。你能检查一下吗?我检查一下,fxml不是空的,路径是正确的。你使用的是哪个版本的JavaFX?尝试将其更新为最新版本。我使用jdk 7.0.51提供的thé版本。我认为openStream()
是合法的,因为load()
方法有重载。fxml变量是一个我不理解的字符串,loader不为null,getResource的返回也不为null。我没有访问iscycle源的权限,很难调试好吧,我有线索。只有当fxml中有一些fx:include标记时,才会发生此异常。但是为什么?我想你可能在某个地方建立了循环依赖关系。我没有任何fx:include标签在我的项目中被使用,你能发布一个小样本fxml,它的标签似乎在你的OP中破坏了它吗?:)你能详细说明一下吗?