gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:54:03 27 4
gpt4 key购买 nike

我遵循了 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com