2015-11-09 45 views
0

我們正在使用JavaFX編寫Java應用程序。在這個時候,我們有3種不同的形式:未知路徑FXML文檔

  • 登錄
  • 遊戲窗口
  • 註冊

對於我們的下一個迭代,我們要實現的登記表,但我們得到的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.fxmlFXMLLoader.fxml時,上述代碼正在工作。

當我改變

mainroot = (Parent)registrationLoader.load(this.getClass().getResource("FXMLRegistration.fxml").openStream()); 

mainroot = (Parent)registrationLoader.load(Paths.get("src/hackattackfx/FXMLRegistration.fxml").toUri().toURL()); 

source

我得到在調試器輸出的絕對路徑,當我在終端的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

在默認包的包:

  • 默認套餐
    • 例外
    • 接口
    • 枚舉
    • 資源
    • 模板
  • JSON包

我FXML文件都位於默認包(hackattackfx)。如果不是100%清楚,我怎麼安排我的文件,請看看my Github repo

+0

你的問題不是很清楚(對我)。當你使用'Paths.get(「src/hackattackfx/FXMLRegistration.fxml」).toUri()。toURL()'它是否工作?你的目標是使用'Paths.get()'?如果是,爲什麼? – ItachiUchiha

+0

Paths.get()不起作用,它將打印到我的調試器的路徑,以便它可以放到我的catch塊中。如果可能,我想使用getClass()。getResource(「X」)。openStream() – Jules

+0

如果文件存在於類路徑中,則應該使用'getClass()。getResource()'。在這種情況下,Paths.get()沒有任何意義。 – ItachiUchiha

回答

1

所以,我得到了所有好奇,找出根本原因,我克隆回購,發現實際的問題是以下錯誤並且被張貼在OP中的問題

由此造成的一個:在hackattackfx.FXMLRegistrationController.initialize(FXMLRegistrationController.java:67)顯示java.lang.NullPointerException

這意味着,在控制器pane爲空。

這是因爲fxml缺少fx:id的聲明。

fx:id="pane"添加到AnchorPane聲明FXMLRegistration.fxml和東西應該工作正常。

+0

感謝您的幫助。當我回到我的辦公桌後,我會嘗試它,並會告訴你它是否有效! – Jules

+0

對不起,但你的答案不適合我。在嘗試您的答案後,我使用結果編輯了我的問題。 – Jules

+0

你可以將項目結構添加到問題中嗎? – ItachiUchiha

0

您需要與/

,這是爲我工作的啓動路徑:

final String fxmlPath = "/fxml/Main.fxml"; 
final FXMLLoader loader = new FXMLLoader(this.getClass().getResource(fxmlPath)); 

Main.fxml位於資源文件夾(我:/src/main/resources/fxml/