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:
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!