尝试创建View时使用afterburner.fx的NullPointer

时间:2018-09-25 08:31:51

标签: java javafx

Afterburner.fx为我损坏了,我似乎无法使其再次正常工作。

在我的主要方法中,我有这个:

 @Override
public void start(Stage primaryStage) throws Exception{
    Map<Object, Object> customProperties = new HashMap<>();
    Injector.setConfigurationSource(customProperties::get);

    final StackPane views = new StackPane();
    customProperties.put("views", views);

    BattleView battleView = new BattleView();
    Parent view = battleView.getView();
    view.setVisible( false );
    view.setId("battle");
    views.getChildren().add(view);

    ...
}

但是,我到达BattleView battleView = new BattleView();行时似乎遇到了异常。 Afterburner fx似乎正在尝试在toString()上评估ui.battle.BattleView,但不喜欢它,请参见下图:

enter image description here

然后在终端中显示:

enter image description here

我没有从类似的问题中找到任何帮助,所以希望有人能指出我正确的方向!救命!

编辑:将Battle.css和Battle.fxml移至resources / ui / battle后出现相同错误:

enter image description here

Edit2:enter image description here

1 个答案:

答案 0 :(得分:1)

由于您使用的是Java 11和模块系统,因此资源输出目录必须与它们所属的模块位于同一输出目录中。

从帖子中的图像来看,您正在使用gradle,并且直接从IDEA构建项目。

您需要告诉IDE将资源复制到相同的输出目录:

//build.gradle

plugins {
  ...
  id 'idea' //add idea plugin
}

...
//put compiled classes and resources in the same directory (change the path if needed)
idea.module.outputDir file("out/production/classes")  

如果您也计划使用gradle进行构建:

//put compiled classes and resources in the same directory (change the paths if needed)
sourceSets.main.output.resourcesDir = "build/classes/java/main"
sourceSets.test.output.resourcesDir = "build/classes/java/test"

,您可能需要打开带有资源文件的程序包,以便框架可以访问它们:

//module-info.java
module YourModuleName {
    ...
    opens ui.battle to afterburner.fx; //add this
}

编辑:

  

“ idea.module.outputDir”中的“想法”显示为灰色,并显示错误“无法解析符号”想法”。

这不是一个错误,但是您可以轻松摆脱它:

//use this format instead of the previous one
idea {
    module.outputDir file("out/production/classes")
}
  

我从尝试加载资源的输入流中获取nullPointer错误,例如:setImage(new Image(getClass()。getClassLoader()。getResourceAsStream(“ images / ui / mineStartButton.png”))));

请勿调用getClassLoader(),否则,即使调用代码在同一模块中(请检查Javadoc),也要使用图像资源打开包装:

//you need to add '/' at the beginning of the path string to make it absolute
getClass().getResourceAsStream("/images/ui/mineStartButton.png")