JavaFX Location is not set error message [duplicate]

I had this problem and found this post. My issue was just a file name issue.

FXMLLoader(getClass().getResource("/com/companyname/reports/" +
report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") +
".fxml"));

Parent root = (Parent) loader.load();

I have an xml that this is all coming from and I have made sure that my class is the same as the fxml file less the word controller.

I messed up the substring so the path was wrong…sure enough after I fixed the file name it worked.

To make a long story short I think that the problem is either the filename is named improperly or the path is wrong.

ADDITION:
I have since moved to a Maven Project. The non Maven way is to have everything inside of your project path. The Maven way which was listed in the answer below was a bit frustrating at the start but I made a change to my code as follows:

FXMLLoader loader = new FXMLLoader(ReportMenu.this.getClass().getResource("/fxml/" + report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") + ".fxml"));

Leave a Comment