[英]Unknown Path FXML document
我們正在使用JavaFX編寫Java應用程序。 目前,我們有3種不同的形式:
對於我們的下一個迭代,我們想要實現Registration表單,但是會收到IOException錯誤Unknown Path
關於這段代碼:
FXMLLoader registrationLoader = new FXMLLoader();
try{
mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream());
Stage registrationStage = new Stage();
Scene scene = new Scene(mainroot);
registrationStage.setScene(scene);
registrationStage.setTitle("Register your account");
registrationStage.show();
} catch(IOException ex)
{
System.out.print(ex.getMessage());
}
當我將FXMLRegistration.fxml
更改為FXMLDocument.fxml
或FXMLLoader.fxml
時,以上代碼FXMLLoader.fxml
。
當我改變
mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream());
至
mainroot = (Parent)registrationLoader.load(Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL());
我在調試器輸出中獲得了絕對路徑,當我將其與終端中的file
命令一起使用時,這是正確的。
我希望有人可以幫助我們解決這個錯誤。
提前致謝!
編輯
我將一些代碼更改為以下內容:
FXMLLoader registrationLoader = new FXMLLoader(getClass().getResource("/FXMLRegistration.fxml"));
mainroot = (Parent)registrationLoader.load();
但這將返回IllegalStateException:未設置位置。 當我在/FXMLRegistration.fxml
之前刪除/
,我進入catch塊,打印文件的完整路徑:
文件:/Users/juleskreutzer/Documents/github/PTS3/HackAttackFX/dist/run1793658053/HackAttackFX.jar!/hackattackfx/FXMLRegistration.fxml
同樣,將路徑更改為src/hackattackfx/FXMLRegistration.fxml
也會得到IllegalStateException:位置未設置。
項目結構
我們在應用程序中使用不同的軟件包。 所有這些軟件包都在默認軟件包中: hackattackfx
默認軟件包中的軟件包是:
我的FXML文檔位於默認程序包(hackattackfx)中。 如果不是100%清楚我如何安排文件,請查看我的Github存儲庫
因此,我很想知道根本原因,克隆了回購協議,發現實際問題是以下錯誤,並且是OP在問題中發布的錯誤
造成原因:hackattackfx.FXMLRegistrationController.initialize(FXMLRegistrationController.java:67)上的java.lang.NullPointerException
這意味着在控制器pane
為空。
這是因為fxml缺少fx:id
的聲明。
添加fx:id="pane"
來的AnchorPane聲明FXMLRegistration.fxml
,事情應該只是正常工作。
您需要以/
開頭路徑
這為我工作:
final String fxmlPath = "/fxml/Main.fxml";
final FXMLLoader loader = new FXMLLoader(this.getClass().getResource(fxmlPath));
Main.fxml
位於資源文件夾中(對我來說: /src/main/resources/fxml/
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.