2015-11-09 63 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/