IllegalArgumentException when loading controller from FXML

后端 未结 1 1421
难免孤独
难免孤独 2021-01-16 18:20

So I\'m coding this chat-client for a school project, and now I need to implement a GUI, using JavaFX. I\'ve built a neat looking GUI in Scene Builder. Whenever the client r

1条回答
  •  爱一瞬间的悲伤
    2021-01-16 18:49

    The syntax url="@..." in your FXML file refers to a path relative to the current FXMLLoader's location property.

    However, when you load the FXML file, you are not providing the FXMLLoader with a location, but an input stream. Thus the location is null and (as seen in the stack trace), your path gets interpreted as

    null/../../../../img/jim.png
    

    To fix this, provide the location to the FXMLLoader, instead of the input stream:

    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("ChatForm.fxml"));
        Pane p = loader.load();
        ChatForm chatForm = (ChatForm) loader.getController();
        chatForm.test();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    This will fix the immediate problem, assuming the path is actually correct. I think you still have other, essentially unrelated, problems, as you never seem to do anything with the scene graph you load (i.e. you never display the pane p).

    0 讨论(0)
提交回复
热议问题