Loading css using JavaFX null pointer exception in eclipse

后端 未结 4 1167
感情败类
感情败类 2021-01-20 15:24

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         


        
4条回答
  •  独厮守ぢ
    2021-01-20 15:26

    Any resource should be on the classpath to be loaded successfully (if it is in the same folder as you welcome class then it is already so). Then you should precede the path to the stylesheet file by '/' symbol, so that it looks like this:

    scene.getStylesheets().add(welcome.class.getResource("/background.css").toExternalForm());
    

    Then it will load successfully.

提交回复
热议问题