0

I try to use SceneBuilder inside Eclipse in order to create a first easy example of graphic interface but I had this error that I cannot solve.

I created the Controller Class through the Eclipse function.

Error

    *xception 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(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
    Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source)
        at ProvaGrafica2.start(ProvaGrafica2.java:29)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
        ... 1 more
    Exception running application ProvaGrafica2*

Code

import java.io.File;

import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ProvaGrafica2 extends Application {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    launch(args);
}
@Override
public void start (Stage stage) throws Exception
{
    System.out.println("prova");

    File file = new File("C:\\Users\\u5ex\\Desktop\\eclipse\\Grafica2\\src\\a.fxml");
    if (file.exists())
        System.out.println("il file esiste");


    Parent root = FXMLLoader.load(getClass().getResource("C:\\Users\\u5ex\\Desktop\\eclipse\\Grafica2\\src\\a.fxml"));
    Scene scene=new Scene (root);
    stage.setTitle("Login");
    stage.setScene(scene);

    stage.show();

}
Daniel
  • 10,641
  • 12
  • 47
  • 85
  • Your `getResource` is returning `null` because your resource path isn't valid. – greg-449 Aug 14 '18 at 15:33
  • Hi Greg, why it is not valid? What should I put? tks – maurizio bortolan Aug 16 '18 at 16:09
  • Resource paths are relative to the class, use '/' not '\' and can't use `C:` – greg-449 Aug 16 '18 at 16:14
  • I put "/a.fxml" also before, but the error is the same. do I need to create special "resources" or something like that? I put the fxml file in the class directory, is it correct? – maurizio bortolan Aug 17 '18 at 06:07
  • Does this answer your question? [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) – kleopatra Jan 28 '21 at 10:01

2 Answers2

1

In your case it should be enough to shorten your getResource call to getClass().getResource("a.fxml"). At least I hope so because without actually trying it I would not bet on it :-)

mipa
  • 10,369
  • 2
  • 16
  • 35
0

@mipa's answer is correct, you should use getClass().getResource("a.fxml"), but here's a little more in-depth about this: getClass().getResource(...) is supposed to be used for files inside the src folder, which files that will end up inside your final JAR. However, the path passed to getResource() is not relative to the src folder but to your class file. So, if your .java file is:

src/com/package/YourClass.java

Then getClass().getResouce("a.fxml") would refer to:

src/com/package/a.fxml

If you want relative to the src directory, start the path with a slash, i.e. getClass().getResouce("/a.fxml").
Also you should note that to load a file that is anywhere on the disk (eg. picked by the user), you should use this instead:

File file = new File("your/path/here/file.fxml");
FXMLLoader.load(file.toURI().toURL());
Chaoz
  • 2,119
  • 1
  • 20
  • 39