Java 尝试创建视图时使用afterburner.fx的空指针

Java 尝试创建视图时使用afterburner.fx的空指针,java,javafx,Java,Javafx,Afterburner.fx对我来说已经坏了,我似乎无法让它再次工作 我的主要方法是: @Override public void start(Stage primaryStage) throws Exception{ Map<Object, Object> customProperties = new HashMap<>(); Injector.setConfigurationSource(customProperties::get); fi

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);

    ...
}
@覆盖
public void start(Stage primaryStage)引发异常{
Map customProperties=newhashmap();
setConfigurationSource(customProperties::get);
最终StackPane视图=新StackPane();
customProperties.put(“视图”,视图);
战场视图战场视图=新战场视图();
父视图=battleView.getView();
view.setVisible(false);
视图.setId(“战斗”);
views.getChildren().add(视图);
...
}
然而,当我进入
BattleView BattleView=newbattleview()时,我似乎遇到了一个异常行。加力fx似乎试图在
ui.battle.BattleView
上评估
toString()
,但它不喜欢,请参见下图:

在终端结果中:

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

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


Edit2:

因为您使用的是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”中的“idea”变灰,错误为“无法解析符号“idea”

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

//use this format instead of the previous one
idea {
    module.outputDir file("out/production/classes")
}
我在试图加载资源的输入流中遇到一个空指针错误,例如:setImage(新图像(getClass().getClassLoader().getResourceAsStream(“images/ui/minestbutton.png”))

不要调用
getClassLoader()
,否则您将需要使用映像资源打开包-即使调用的代码在同一模块中(选中):


请将您的资源文件(fxml和css)移动到
src/main/resources/ui/battle
,然后重试。@JoséPereda,我也有同样的错误。在图片中编辑,以便您可以看到。也谢谢你的建议!我的印象是,按照惯例,AfterburnerFX要求它们彼此相邻。您这样做完全是因为您使用相同的包名,但对于Maven/Gradle项目,您必须将源类与资源分开。至于错误,请注意,它抱怨的是
Battle.fxml
,而不是
Battle.fxml
,这很奇怪,因为它应该寻找全小写的名称。你的加力版本是什么?您可以将其重命名为
Battle.fxml
并重试吗?重命名时出现相同的错误。谢谢Maven/Gradle的信息,我不知道。(更新这个没有使用模块/任何构建工具/Java11/JavaFX11的旧项目,我是新手)我使用的是1.7.0。你检查了整个stacktrace吗?通常错误出现在最后,这是因为加载FXML时出现故障。“idea.module.outputDir”中的“idea”因错误“无法解析符号“idea”而变灰。尽管加力燃烧室现在似乎能够阅读所有内容,除了。。我在尝试加载资源的输入流中收到一个空指针错误,例如:setImage(newImage(getClass().getClassLoader().getResourceAsStream(“images/ui/minestbutton.png”));在OP中的屏幕截图中,您可以看到上面URL指向的原始资源文件夹很好,为什么它不再工作了?试图将整个文件夹复制到新的资源文件夹,但仍然没有任何结果…我已编辑了错误图片。我认为这是最后一件要解决的事情!隧道尽头有亮光(我想!)感谢迄今为止所做的一切:)神圣的蝙蝠球你只是去做了而已。我已经为这一切挣扎了大约一周,现在一切都正常工作,正在加载,我也学到了很多东西。非常感谢,伙计,你真的挺过来了,有一天我会尽我最大的努力通过它:)
//you need to add '/' at the beginning of the path string to make it absolute
getClass().getResourceAsStream("/images/ui/mineStartButton.png")