How can I make the CSS work on my Java project? [duplicate]

I’m trying to implement a CSS document into my Java project. I’m using JavaFX as my graphic interface.

My code is:

Scene escena = new Scene(grid, 500, 500);
escena.getStylesheets().add(getClass().getResource("estilos.css").toString());

The error is:

Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toString()" because the return value of "java.lang.Class.getResource(String)" is null

If tried to change the CSS document name, and some changes in the getStylesheets call, but it says that I’m obtaining a null pointer exception.

  • As specified in the duplicate (assuming it is in src/main/resources of your project), use: escena.getStylesheets().add("estilos.css").

    – 

  • Now it shows this error dic 01, 2023 1:33:46 P.�M. com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged ADVERTENCIA: Resource “estilos.css” not found.

    – 

  • Where exactly is the resource located? If it is for example in src/main/resources/my/package/estilos.css, you probably need to use /my/package/estilos.css or my/package/estilos.css.

    – 




Leave a Comment