0

A little confused with a NullPointerException that I am getting. I am trying to add a .css file to my Main class. Here is the code.

        //Load FXML file
        Parent root = FXMLLoader.load(getClass().getResource("/application/HumansightFirstDraft.fxml"));

        // Create scene
        Scene scene = new Scene(root, 400, 600);

        //Load CSS
        scene.getStylesheets().add(getClass().getClassLoader().getResource("/application/application.css").toExternalForm());

        // Display application
        primaryStage.setScene(scene);
        primaryStage.show();`

The application.css is in the same place as the Main.java.

Here is my console output.

Jan 10, 2019 8:30:20 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 10.0.1 by JavaFX runtime of version 8.0.191
java.lang.NullPointerException
    at application.Main.start(Main.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

Here is my css. I only have one change so as to make it easier to test.

#startSurvey {

-fx-border-radius: 30;

}

Here is my directory as seen in Eclipse.

Screenshot of folder

10
  • 1
    When the result of getResource(...) is null that means the resource couldn't be found at the specified location. Make sure you have the path correct and that the resource file is on the classpath. Commented Jan 10, 2019 at 14:17
  • I assumed that much, but the I've checked and rechecked the file path and it seems to be correct. I will include a screenshot of my folder. Commented Jan 10, 2019 at 14:22
  • What happens if you use getClass().getResource(...) instead of getClass().getClassLoader().getResource(...)? Commented Jan 10, 2019 at 14:24
  • The fact that the css file is included in the source folder does not automatically result in the IDE copying it to the build folders. Have you checked the folders containing the .class files for the css file? Commented Jan 10, 2019 at 14:27
  • So when I take out getClassLoader(), there is no error, but the css doesn't load. I've had a weird thing since I started JavaFX where my programs only work when I include the getClassLoader(). Where would I find these folders? Commented Jan 10, 2019 at 14:36

1 Answer 1

1

You dont nee to say /application/application.css

Just write it like this application.css

This is becsuse both the main class an the css file are in the same parent direvtory

The same as all the fxml files

Update It is also possible that the null pointer occurs in the controller of the fxml.

Update 2 Thanks to

@kleopatra

dont use getClass().getClassLoader().getResource() simply use getClass().getResource()

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

7 Comments

That's what I thought too. But I tried that out and it didn't change.
beware, the getResource param interpretation is different for classloader vs class: classloader.getResource(absolutePath) - can have the leading slash or not, doesn't matter, vs. class.getResource(relativePath) without slash and absolute with slash. So something really fishy going on here ... most probably a head-on-table when found ;)
that's not what I meant (probably wasn't clear enough): we can use the one or other but must be aware of the difference :)
So, when I do what you said, I get no errors, but the nothing from the css is actually applied.
Your correction was right. The problem ended up actually being in my CSS. The -fx-border-radius characteristic does not change what I expected, so the css was applied, but in a visible way. But thank you for teaching me about the difference between class... & class.classLoader()...
|

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.