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

标签 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 似乎试图在 ui.battle.BattleView 上评估 toString(),但它不喜欢它,请参见下图:

enter image description here

在终端结果中:

enter image description here

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

编辑:将Battle.css和battle.fxml移动到resources/ui/battle后出现同样的错误:

enter image description here

编辑2:enter image description here

最佳答案

由于您使用的是 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
}

编辑:

The 'idea' in 'idea.module.outputDir' is greyed out with the error 'Cannot resolve symbol 'idea''.

这并不是一个真正的错误,但您可以轻松地消除它:

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

Im getting an nullPointer error from input streams where I am trying to load resources, such as: 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")

关于java - 尝试创建 View 时使用 afterburner.fx 的 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52493819/

相关文章:

JavaFX 将结果集显示到现有表格 View 中

JavaFX JDK 9.0.4 ListView celFactory 添加空单元格

java - 我们可以使用反射调用抽象类的私有(private)方法吗?

java - 使用 JAXB : [ERROR] Unexpected <xsd:element> appears with an XSD file 时出现问题

java - 你怎么能像 Jackson 的 @JsonAnySetter 一样得到 Gson 中无法识别的 JSON 字段列表?

java - Java 中的同步块(synchronized block)是如何实现的?

java - 尝试使用 GTKLookAndFeel 在 JavaFX 中设置 SwingNode 的样式会卡住应用程序

Javafx - 我无法将数据放入 TableView

java - 无论是否部署 war ,tomcat8 上的高 CPU

java - 如何向 JavaFX 图表添加辅助图例?