I've been going through a tutorial to try and learn more about JavaFX while working with scenebuilder, and plan on practicing writing methods by adding more to it. However, while I've found plenty of people have found the same issue as I have, none of their fixes seem to work.
Code:
package login;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
private static Stage stg;
@Override
//This method initializes the login page
public void start(Stage primaryStage) throws Exception {
stg = primaryStage;
primaryStage.setResizable(false);
Parent root = FXMLLoader.load(getClass().getResource("C:\\Users\\user\\OneDrive\\Desktop\\Java Projects\\Eclipse Login\\src\\login"));
primaryStage.setTitle("Pantry");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
//This method will change the scene to the next page after logging in
public void changeScene(String fxml) throws IOException{
Parent pane = FXMLLoader.load(getClass().getResource(fxml));
stg.getScene().setRoot(pane);
}
public static void main(String[] args) {
launch(args);
}
}
Results:
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:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml@18/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at login.Main.start(Main.java:20)
at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@18/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application login.Main
After reading around, I've come to understand this error means that my login.fxml isn't found by the program, which is where I'm stuck. I've not only used the absolute path to it, but it's also located in the same directory as the fxml. Any advice is appreciated, because I'm certain there's something I've missed somewhere.
CodePudding user response:
Instead of FXMLLoader.load(getClass().getResource()
Try to use that :
FXMLLoader.load(getClass().getClassLoader().getResource("login.fxml"))
CodePudding user response:
The issue is being caused by getResource function, you see getResource function gets you location of a file from class-path, so it's trying to search for your specified file in your class-path and it won't find it because you don't have any file by that name.
Replace your URL with just "login.fxml"
and it should work.