I\'m trying to load a CSS file into JavaFX using this line of code and it gives me a null pointer exception:
scene.getStylesheets().add(welcome.class.getReso
So I know this an old question, but I had a similar issue to this recently so I'd like to give what I think is the answer:
Three conditions must be met to use a JavaFX file and a CSS file together:
You must declare in your code you are using the CSS file such as
scene.getStylesheets().add(welcome.class.getResource("background.css").toExternalForm());
And the thing I think was being left out is package PackageName;
package YourPackageName;.
Will be at the top of your JavaFX file before the place where you define your imports. This is something that could be easily forgotten if you are not used to working with multiple files in Java or if you start your code from scratch. Not having the package YourPackageName; does not stop your base file from working, but it can stop your program from running when you try to use your own defined CSS files.