3

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.getResource("background.css").toExternalForm());

My background.css is located in the same folder as the welcome class I have made.

Any idea why I get a null pointer?

Error Log:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at welcome.start(welcome.java:164)
    at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    ... 1 more
Kimmy
  • 3,787
  • 5
  • 22
  • 20
  • Does `System.out.println(welcome.class.getResource("background.css"));` print `null`? Is your eclipse project copying the `background.css` file to the output directory (the same directory as the file `welcome.class`)? – jewelsea Mar 06 '13 at 01:17
  • What is your IDE ? Try to do a full rebuild of the project. – gontard Mar 06 '13 at 09:59

6 Answers6

5

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.

akhilless
  • 3,169
  • 19
  • 24
2

Did you initialize the Scene object yet?

//Create a scene object. Pass in the layout and set with and height
this.scene = new Scene(layout, 600, 400);

//Add CSS Style Sheet (located in same package as this class).
String css = this.getClass().getResource("background.css").toExternalForm();
scene.getStylesheets().add(css);
medokr
  • 441
  • 1
  • 5
  • 16
  • yes i have public void start(final Stage primaryStage) { final GridPane grid = new GridPane(); Scene scene = new Scene(grid, 600, 500); scene.getStylesheets().add(welcome.class.getResource("background.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); – Kimmy Mar 06 '13 at 01:04
  • Can you paste the code referenced at welcome.java line #164 You might read up on [null pointer exceptions](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – medokr Mar 06 '13 at 01:07
  • scene.getStylesheets().add(welcome.class.getResource("background.css").toExternalForm()); – Kimmy Mar 06 '13 at 01:07
  • could it be something to do with eclipse not have JavaFx setup properly? – Kimmy Mar 06 '13 at 01:08
  • I get a WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "C:\Users\Kimmy\background.css" not found. – Kimmy Mar 06 '13 at 01:21
  • So do you now get an NPE or the warning? It also looks like you are: * using the default package which could be the problem * you are not following Java-Conventions for classnames (should not not be a problem in general) – tomsontom Mar 06 '13 at 06:27
1

So I know this an old question, but I had a similar issue to this recently so I'd like to give what I think is the answer:

Three conditions must be met to use a JavaFX file and a CSS file together:

  1. both JavaFX file and CSS file must both be in same directory
  2. You must declare in your code you are using the CSS file such as

      scene.getStylesheets().add(welcome.class.getResource("background.css").toExternalForm());
    
  3. And the thing I think was being left out is package PackageName;

    package YourPackageName;.
    Will be at the top of your JavaFX file before the place where you define your imports. This is something that could be easily forgotten if you are not used to working with multiple files in Java or if you start your code from scratch. Not having the package YourPackageName; does not stop your base file from working, but it can stop your program from running when you try to use your own defined CSS files.

user3003304
  • 288
  • 1
  • 6
  • 18
1

I had the same error than you... And here is the solution I found... if you have a Main class , replace the "welcome" by Main to get this code:

The tree in Eclipse with JavaFX 2.0:

-MyJavaProject            (JavaProject)
 | -src                   (Folder)
     | -Appli             (Package)
        |  -Main.java     (Class Main)
        |  -application.css  (css file)

code for this tree:

scene.getStylesheets().add(
          Main.class.getResource("application.css").toExternalForm());

Hope this will help... ;)

Corentin
  • 11
  • 1
0

Also, depending on how you setup maven, you need to place the files as follows (in the source folder):

-main
    | -java
        | -packageName
            |  -Main.java
    | -resources
        | -packageName
            |  -application.css 
moser
  • 446
  • 3
  • 14
0

This worked for me, use this directly getStylesheets().add("path/name/application.css")

your pathname may differ

Gideon
  • 1