java - JavaFX 尝试创建新对话框并将其中的进度条绑定(bind)到工作线程时出现错误

标签 java javabeans javafx

我遵循了 StackOverflow 上另一个答案的建议 Passing Parameters JavaFX FXML ,但是当我尝试运行我的程序时,我遇到了空指针异常。

我有代码可以创建一个新的工作线程并启动一个线程,然后尝试显示一个探查器对话框。但是,该对话框无法正确打开。

@FXML
private void profilePDBFoldertoCSVAction(ActionEvent e) {
PDBProfilerOperator worker = new PDBProfilerOperator();
FileChooser fc = new FileChooser();
DirectoryChooser dc = new DirectoryChooser();
Stage s = new Stage();
worker.setPdbsdirectory(dc.showDialog(s));
worker.setOutputCSV(fc.showSaveDialog(s));
Thread th = new Thread(worker);
th.setDaemon(true);
th.start();
worker.showProfilerDialog(worker);
}

showProfilerDialog 如下:

public Stage showProfilerDialog(PDBProfilerOperator operator) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/pdpro/gui/dialogues/dataset/ProfilingProgress.fxml"));
ProfilingProgressController controller = loader.<ProfilingProgressController>getController();
controller.initProgress(operator);
Parent root = null;
try {
  root = (Parent) loader.load();
} catch (IOException ex) {
  Logger.getLogger(PDPro.class.getName()).log(Level.SEVERE, null, ex);
}
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.setTitle("Profiling Progress");
stage.show();
return stage;
}

initProgress 如下:

public void initProgress(PDBProfilerOperator operator) {
  this.profilingFiles.progressProperty().bind(operator.progressProperty());
}

由于 Controller 保持为 Null,因此尝试运行 initProgress 时出现 NullPointerException。如何修复此错误?

谢谢!

最佳答案

我之前对 Passing Parameters JavaFX FXML 的回答中的代码不正确 - 在从 FXMLLoader 检索 Controller 之前必须加载 fxml。我更新了答案中的错误代码。

要对 Kylamus 的代码应用相同的修复:

public Stage showProfilerDialog(PDBProfilerOperator operator) {
  try {
    FXMLLoader loader = new FXMLLoader(
      getClass().getResource(
        "/pdpro/gui/dialogues/dataset/ProfilingProgress.fxml"
      )
    );
    Parent root = (Parent) loader.load();

    ProfilingProgressController controller = 
      loader.<ProfilingProgressController>getController();
    controller.initProgress(operator);

    Stage stage = new Stage(new Scene(root));
    stage.setTitle("Profiling Progress");
    stage.show();

    return stage;
  } catch (IOException ex) {
    Logger.getLogger(PDPro.class.getName()).log(Level.SEVERE, null, ex);
  }

  return null;
}
<小时/>

有一个可执行文件sample我在How can I use a variable from another Controller in JavaFX的答案中使用了类似的结构。

<小时/>

可能导致加载 fxml 文件失败的其他可能原因如下(尽管这些都不是本例中的实际原因):

  1. 您的 fxml 格式错误 => 在 SceneBuilder 中打开它并查看 SceneBuilder 是否注意到任何错误。
  2. 您的 fxml 不在应有的位置 => getClass().getResource("/pdpro/gui/dialogues/dataset/ProfilingProgress.fxml") 返回 null 吗?
  3. 您的 fxml 未引用您的 Controller => 请参阅 how to reference a controller .

关于java - JavaFX 尝试创建新对话框并将其中的进度条绑定(bind)到工作线程时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14471353/

相关文章:

java - 使用Spring框架的构造函数依赖注入(inject),无法保留值

eclipse - 无法在 Eclipse 中为 JavaFX 添加 css 文件

java - 如何在REST Web服务中处理带有多个参数的HTTP请求+限制数字范围

java - libGDX API 更改后,舞台上的 Actor 不再接收 touchDown

java - Bean Validation Groups - 正确理解

JavaFX 2.2 Stage 始终在最前面

JavaFX:禁用 TableView 的水平滚动条

java - 从其他对象创建新对象时 instanceOf 的问题

java - ant 文件的问题

java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No bean named 'customerService' is defined