[英]NullPointerException load 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.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>
這是解決方案。
為加載器添加以下行:
loader.setLocation(JfxUtils.class.getResource(fxml));
非常糟糕的錯誤。
加載fxml的正確語法是:
FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));
你的NullPointerException的原因可能是由於我沒有看到你的fxml
變量的任何定義。 另外,我不確定你的.openStream()位在你的最后。
有關FXML的更多信息,請參閱此處 。
編輯
根據評論,有3個左右的地方,我看到你可以得到這個NPE。
地方#1)加載器為空,因為我們可以看到它顯然不是,這是一個非問題。 地點#2)JfxUtils是一個不存在的項目,但根據錯誤,這也不是。 位置#3 getResource(fxml)返回null,因為它指向不存在的東西,因此返回null,並且您無法在null對象上打開流。
這一行: at javafx.fxml.FXMLLoader.isCyclic(FXMLLoader.java:1868)
中的例外情況對我來說很突出,雖然我沒有時間在第二個潛入並探索它。 我內心的某些東西告訴我一些奇怪的原因可能與它有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.