1

I followed the advice of another answer on StackOverflow Passing Parameters JavaFX FXML, but when I attempted to run my program, I got a null pointer exception.

I have code that makes a new worker and starts a thread and then attempts to show a profiler dialog. However, this dialog does not open properly.

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

The showProfilerDialog is the following:

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

And the initProgress is the following:

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

I get a NullPointerException when attempting to run the initProgress since the controller remains Null. How do I fix this error?

Thanks!

Community
  • 1
  • 1
calben
  • 1,328
  • 3
  • 18
  • 33

1 Answers1

0

The code in my previous answer to Passing Parameters JavaFX FXML was incorrect - the fxml must be loaded before retrieving the controller from the FXMLLoader. I updated the incorrect code in the answer.

To apply the same fix to Kylamus' code:

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

There is an executable sample where I make use of similar constructs in the answer to How can I use a variable from another Controller in JavaFX


Other possible reasons which can cause failure to load fxml files are the ones below (although none of these were the actual reason in this case):

  1. Your fxml is malformed => open it in SceneBuilder and see if SceneBuilder notes any errors.
  2. Your fxml isn't where it's supposed to be => Does getClass().getResource("/pdpro/gui/dialogues/dataset/ProfilingProgress.fxml") return null?
  3. Your fxml does not reference your controller => see how to reference a controller.
Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • My FXML seems to have 0 errors and opens in SceneBuilder 1.0 and 1.1. getClass() doesn't return a null and a System Out Print on it returned this: jar:file:/X:/Work/PDPro/pdpro/dist/pdpro.jar!/pdpro/gui/dialogues/dataset/ProfilingProgress.fxml My FXML references the controller here: fx:controller="pdpro.gui.dialogues.dataset.ProfilingProgressController" and SceneBuilder recognizes the different methods in it in dropdown menus. – calben Jan 23 '13 at 16:56
  • Is there anything else I should test for?! Thanks, JewelSea! – calben Jan 25 '13 at 05:31