0

I am trying to add CSS to a JavaFX project that I am developing in IntelliJ. I have a CSS style-sheet named "list-view.css" and want to attach it to my project. Here's a screenshot of my project's directory:

Project Directory

The file is located in the same directory as the Main file that I have attached the code for. When I print out the value of the variable url, I keep on getting "null" and keep getting the following error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
    at com.kernaldrive.startupscreen.Main.start(Main.java:72)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application com.kernaldrive.startupscreen.Main

I checked out this answer: How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?

But this was not able to solve it unfortunately. Here is my code, any help would be greatly appreciated:

public void start(Stage primaryStage) throws Exception {

    primaryStage.initStyle(StageStyle.UTILITY);     //removes minimize and fullscreen button

    Rectangle2D screenBounds = Screen.getPrimary().getBounds();
    double screenWidth = screenBounds.getWidth();
    double screenHeight = screenBounds.getHeight();
    primaryStage.setTitle("Homepage - KernalDrive");

    Pane layout = new Pane();

    Scene scene = new Scene(layout, 1920, 1080);
    URL url = getClass().getResource("listView.css");
    String css = url.toExternalForm();
    scene.getStylesheets().add(css);

    primaryStage.setScene(scene);
    primaryStage.setMaximized(true);
    primaryStage.show();
}

EDIT: I have fixed the URL url = getClass().getResource("listView.css"); but am still getting the same issue!

  • In the answer to [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) did you read the "_Maven (and similar) standard layouts_" section? Because based on your project directory layout you're using Maven, Gradle, or something similar yet you erroneously have the CSS file in the `src/main/java` directory. – Slaw Nov 23 '20 at 03:44
  • In other words, try having the file as `src/main/resources/com/kernaldrive/startupscreen/list-view.css` and then `Main.class.getResource("list-view.css")` should work for you. – Slaw Nov 23 '20 at 03:49
  • now what _is_ the name of your css? clean out the project and do a clean build from scratch. If it persists, follow the troubleshooting section of the duplicate. – kleopatra Nov 23 '20 at 11:43

1 Answers1

0

You have to type in the same name as the file:

you typed listView.css but your file is called list-view.css

now your code looks like this:

URL url = getClass().getResource("list-view.css");
Maxi
  • 207
  • 3
  • 15
  • I tried this but it still hasn't fixed the issue unfortunately – Ahmed Nuur Nov 22 '20 at 23:03
  • okay, this works for me, i recreated the problem on my pc and this fixes it but i am not working with maven or else in my javafx project – Maxi Nov 23 '20 at 06:49