0

I have a JavaFx OSGi project:

src
    main.java.ui.impl
        UI.java
    main.resources
        ui.css

I'm getting a null pointer whene I try to link up the Scene to the CSS file ui.css as follows:

scene.getStylesheets().add(this.getClass().getResource("../../../ui.css").toExternalForm());

I've even tried to have the ui.css in the folder/ package ui directly above the package impl where UI.java is located, but still null pointer

scene.getStylesheets().add(this.getClass().getResource("../ui.css").toExternalForm());

It only works where all the files are placed in the same package:

scene.getStylesheets().add(this.getClass().getResource("ui.css").toExternalForm());

What is the right way to go about this? What am I doing wrong?

Thank you all in advance.



UPDATE

Source repository on GitHub >>> javafx-osgi-example-master

Be sure to first install Amdatu from the Eclipse marketplace

1 Answer 1

1

Specify it relative to the class path, either by starting the path with a leading /, or by retrieving the resource from the ClassLoader:

scene.getStylesheets().add(this.getClass().getResource("/ui.css").toExternalForm());

or

scene.getStylesheets().add(this.getClass().getClassLoader().getResource("ui.css").toExternalForm());

The problem with your code as it stands is that you are not providing a valid resource name according to this specification. In particular:

A resource is identified by a string consisting of a sequence of substrings, delimited by slashes (/), followed by a resource name. Each substring must be a valid Java identifier.

and, of course, .. is not a valid Java identifier.

Sign up to request clarification or add additional context in comments.

3 Comments

What will it be if it is one package up? "/../ui.css"?
It can't be one package up from the root of the classpath, by definition of "root".
Hi @James_D Thank you for the responses you gave to my question. Your suggestions should be working, but for some reason they won't work with my project. I updated my question with a link to the project source files on GitHub. (javafx-osgi-example-master). I was hoping you could take a look and offer a suggestion. Thank you for your help so far

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.