The short answer is getClass().getResource("sample.fxml")
returns null
silently if the resource cannot be found on the runtime classpath, not the current directory etc.
So this depends on your IDE project setup, if you’re using eclipse try adding the folder that sample.fxml
resides in the run configuration.
Some ideas…
- try
getClass().getResource("/sample.fxml")
instead… - try moving
sample.fxml
into the resources folder. I don’t know much about your IDE, but I suspect that folder is only used for.java
files… this is certainly true for gradle projects in eclipse – resources have to be in thesrc/main/resources
tree as only that is added to the runtime classpath…