0

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145

1 Answers1

-1

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.

Hiten Tandon
  • 64
  • 1
  • 5
  • 1
    Your answer is mostly correct, but the resource will be loaded from class-path not relative to source folder. – seenukarthi Mar 16 '22 at 08:11
  • Thank you for the answers, though I do want to comment I initially had the absolute path as just the file name (login.fxml) and it didn't work then either. I did try Oussama's suggestion, but unless I implemented it wrong then that didn't work either. The same long list of errors in the stack show up. – Download Fanatic Zeb Mar 16 '22 at 08:24
  • @DownloadFanaticZeb Comment all of commands in start method and try printing getClass().getResource("login.fxml") then share the result with me... – Hiten Tandon Mar 16 '22 at 09:07
  • Thank you for getting back to me. After removing the method's contents and running just getClass().getResource("login.fxml"); there were no errors, and the application stopped after running that method. I also got a blank screen to show up by changing my path when assigning the root but that doesn't match my fxml page. – Download Fanatic Zeb Mar 16 '22 at 20:57
  • @DownloadFanaticZeb Just in case, can you add the fxml file's code to the question? I think, the problem may lie there, because, if your class can find it, and can't access it, it may be due to the FXML file's contents... – Hiten Tandon Mar 17 '22 at 04:13