HarpoN
HarpoN

Reputation: 73

Getstylesheets is giving a nullpointerexception in Javafx, how do I fix this?

I am well aware that this question has been asked before (several times), but I've reviewed the 'solutions' on what I could find (adding css file to stylesheets in javafx, Loading css using JavaFX null pointer exception in eclipse, CSS stylesheet not loading) and nothing has seemed to work.

Here is the line of code in question:

scene.getStylesheets().add(getClass().getResource("MainMenu.css").toExternalForm()); //this code gives a nullpointerexception

In every other project I have had this code has worked fine, so I don't understand what the problem is. The only difference I can think of is that this is within a git repository.

To answer some questions that I've seen asked:

Yes, I have initialized the scene, the scene is not the problem and the program runs fine without the line of code above.

Yes, I have tried adding a backslash before the name. It did not work

Yes, the css file is in the same folder as the .java file

Yes, I have included the package at the top of the .java file

The file is purely named "MainMenu.css", not "MainMenu.css.txt" or "MainMenu.css.css"

I can't for the life of me understand where the problem is. Is it a typo? Did I miss something? Help would really be appreciated.

If it helps any, I'm using Java 8 with Maven through Visual Studio Code.

Upvotes: 0

Views: 873

Answers (1)

minus
minus

Reputation: 2786

The problem lies in getClass() which resolves resources relative to the path of the class.

So getClass().getResource("MainMenu.css") will resolve to the correct URL only if is in the same package dir of the class of the object which is using that instruction.

So if MainMenu.css is in the same package (directory) of a MainMenuView class, just use MainMenuView.getResource("MainMenu.css") , otherwise you can always use absolute paths, like

getClass().getResource("/org/mycompany/myapp/view/MainMenu.css")

Update

Maven could also be the reason why you could not find your resource file.

Maven does not move resources placed in the src/main/java folder into the target/classes folder.

It does move resources in the src/main/resources folder.

If you have a standard maven layout this could lead resources missing.

You could, in that case, use the maven-resource-plugin to include those resources in your build.

For example, in one project I had to add this plugin configuration for the build.

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-fxml-resources</id>
                        <!-- here the phase you need -->
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/java/</directory>
                                    <filtering>false</filtering>
                                    <includes>
                                        <include>**/*.fxml</include>
                                        <include>**/*.css</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

I needed the resources in the src/main/java folder, because otherwise the Scene Builder would not find them.

Upvotes: 2

Related Questions