Get Off My Lawn
Get Off My Lawn

Reputation: 36351

JavaFX CSS URL isn't loading

I am trying to load an XML Resource, and I am doing it like this:

fxmlLoader = new FXMLLoader();
root = fxmlLoader.load(getClass().getResource("Document.fxml").openStream());

When I run my code I then get this error:

null/../css/button.css
javafx.fxml.LoadException: 
unknown path:23

When I look at line 23 I have this:

<URL value="@../css/button.css" />

this works:

fxmlLoader = new FXMLLoader();
root = fxmlLoader.load(getClass().getResource("Document.fxml"));

but then when I run the following

controller = (DocumentController)fxmlLoader.getController();

controller is null

How can I fix the css issue?

Upvotes: 1

Views: 1220

Answers (2)

Ben T
Ben T

Reputation: 1

In your .fxml, change <URL value = "@../css/button.css"/> to <URL value = "@/css/button.css" />. It's the same if you had "css" or "image".

Upvotes: 0

James_D
James_D

Reputation: 209704

This is a bit of a guess, but I think the issue is that you're providing an input stream to the FXMLLoader instead of a URL. Because of this, the FXMLLoader is unaware of the location of the FXML resource, and hence it can't resolve the .. in the URL tag. This would explain the error message:

null/../css/button.css

javafx.fxml.LoadException:

unknown path:23

The path you provided is relative to null because the FXMLLoader doesn't know the location of the FXML file; notice also it reports "unknown path" as the source of the FXML.

Try this (which is more common anyway)

fxmlLoader = new FXMLLoader(getClass().getResource("Document.fxml"));
root = fxmlLoader.load(); 

I'm figuring it returns null for the controller only because there was an error in loading the fxml.

Upvotes: 4

Related Questions