1

It must be a very simple thing to do perhaps but some how none of the solutions worked for me so far. So finally the question, maybe something that I didn't take in account.

I want to load FXML with its controller from sub package of the start class in Netbeans project. Tried all the solutions here referring to many different questions already, still didn't work.

The package structure:

Source Pacakge
    -a
        -b
            -c
                -d
                    StartUp_Classs.java
                    -ui
                        FXMLDocument.fxml
                        FXMLDocumentController.java

Here is the start method:

@Override
public void start(Stage stage) throws Exception {

    try {
        setUserAgentStylesheet(STYLESHEET_MODENA);
        FXMLLoader loader = new FXMLLoader();
        Parent root = (Parent) loader.load(getClass().getResourceAsStream("/ui/FXMLDocument.fxml"));
        final FXMLDocumentController controller = (FXMLDocumentController) loader.getController();

        stage.addEventHandler(WindowEvent.WINDOW_SHOWN, controller::handleWindowShownEvent);
        stage.addEventHandler(WindowEvent.WINDOW_SHOWING, controller::handleWindowShowingEvent);

        Scene scene = new Scene(root);

        stage.setScene(scene);

        stage.setResizable(false);
        stage.toFront();
        stage.setTitle("Simple FXML");
        stage.getIcons().add(new Image(getClass().getResourceAsStream("/resources/images/Orange.jpg")));
        stage.show();
    } catch (IOException iOException) {
        iOException.printStackTrace();
    }
}

Any suggestions, would be a great help.

Indigo
  • 2,887
  • 11
  • 52
  • 83

2 Answers2

1

You might delete the leading / in your path String for the .fxml file.

Parent root = (Parent) loader.load(getClass().getResourceAsStream("ui/FXMLDocument.fxml"));
ifloop
  • 8,079
  • 2
  • 26
  • 35
  • nope tried that one as well, got this in return `Exception in Application start method java.lang.RuntimeException: Exception in Application start method` – Indigo Sep 14 '14 at 11:46
  • @Indigo is there a nested exception? If so, please let me know. – ifloop Sep 14 '14 at 11:47
  • ohh sorry, indeed there is one, Caused by: java.lang.NullPointerException: inputStream is null. – Indigo Sep 14 '14 at 11:48
  • 1
    @Indigo This might be trivial but, did you try to move the target .fxml file some where else and back there again (with appopriate refactroing by your IDE)? Maybe you want to .zip you project and share a link from where we can download it for a closer look? – ifloop Sep 14 '14 at 11:52
  • Found the problem. It was hidden in FXML file. While Netbeans refactored files, it still didn't update the Controller class path in FXML file and that was the root of error. After I read through the FXML, it was clear. I am quite sure that I didn't move any single file manually, but then doesn't it make a Netbeans bug though? **It was like this:** `fx:controller="OldPacakgeDefault.FXMLDocumentController"` **It must have been this:** `fx:controller="a.b.c.d.ui.FXMLDocumentController"` Thanks for the help – Indigo Sep 14 '14 at 12:27
  • @Indigo Great your problem got solved and even geater you outlined the actual bug in an own answer (Would have been hard to find for others in this list of comments under my post). +1 for that. – ifloop Sep 14 '14 at 12:44
  • always good to get help but also to return the favor :-) Hope others would be able to save some precious time using that. – Indigo Sep 14 '14 at 13:14
1

In addition to ifLoop's correct answer above, just an additional hint if someone might face the same problem again.

If you refactor packages in Netbeans, it is not likely that Netbeans would update the Controller class path in FXML. So better go check correct class path in fx:controller= attribute at the beginning of FXML file and manually correct it.

In my case after refactoring packages:

It was like this:

fx:controller="OldPacakgeDefault.FXMLDocumentController"

It must have been this:

fx:controller="a.b.c.d.ui.FXMLDocumentController"

Community
  • 1
  • 1
Indigo
  • 2,887
  • 11
  • 52
  • 83