0

I tried to build a simple scene by using scene builder. But i failed to make a main.java file to run the code! i can't understand what's the wrong!

Here's my FXMLDocumentController.java file:

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

public class FXMLDocumentController {

    @FXML
    void btnHandle(ActionEvent event) {
    }

}

And here's the fxml file: sample.fxml

And the Main.java

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override  
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        launch(args);
    }  
}

When i run this code it show error and failed to build: Exception in Application start method

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at Main.start(Main.java:15)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application Main
C:\Users\Dell\Desktop\Exercise\Java\BasicXml\nbproject\build-impl.xml:1052: The following error occurred while executing this line:
C:\Users\Dell\Desktop\Exercise\Java\BasicXml\nbproject\build-impl.xml:806: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Lucas Licursi
  • 42
  • 1
  • 8
Tonmoy Mohajan
  • 91
  • 2
  • 4
  • 13
  • 2
    Telling us the exact error would be helpful. – takendarkk Jul 13 '17 at 20:33
  • When i run the code it show error::: Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ............. BUILD FAILED (total time: 0 seconds) – Tonmoy Mohajan Jul 13 '17 at 20:36
  • Please post the _whole_ stacktrace, and do it in the question, not a comment. – takendarkk Jul 13 '17 at 20:37
  • Now the post is updated. Please check it out – Tonmoy Mohajan Jul 13 '17 at 20:41
  • Well, now that I can see the actual error I [googled](https://www.google.com/search?q=fxml+Location+is+required&rlz=1C1GGRV_enUS750US750&oq=fxml+Location+is+required&aqs=chrome..69i57j0l4.1295j0j7&sourceid=chrome&ie=UTF-8) it and got many results - here is the top one [JavaFX “Location is required.” even though it is in the same package](https://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package) – takendarkk Jul 13 '17 at 20:44
  • There is also [“Location is required” exception when loading FXML file](https://stackoverflow.com/questions/28266492/location-is-required-exception-when-loading-fxml-file), and [JavaFX and maven: NullPointerException: Location is required](https://stackoverflow.com/questions/22000423/javafx-and-maven-nullpointerexception-location-is-required), and [Javafx location is required (loading FXML file from another folder/package)](https://stackoverflow.com/questions/28263941/javafx-location-is-required-loading-fxml-file-from-another-folder-package), and many others. – takendarkk Jul 13 '17 at 20:46
  • 1
    Please let us know what exact things from all of those you have tried, what the results were, and where your confusion still remains. – takendarkk Jul 13 '17 at 20:47
  • `fx:controller="FXMLDocumentController">` <- This code appears incorrect. My `FXML` files nomally have a structure like -> `fx:controller="javafxapplication73.FXMLDocumentController">`. – SedJ601 Jul 13 '17 at 21:55
  • @SedrickJefferson The controller doesn't appear to be in a package, so it may well be correct. But anyway, just read the stack trace, that is clearly not causing the current error. – James_D Jul 13 '17 at 23:32
  • 1
    Either the FXML file is not in the same package as the `Main` class, or the fxml file is not getting deployed to the application build directory. – James_D Jul 13 '17 at 23:34
  • Thanks for the correction. I use Netbeans, so my `FXML` files always have this structure when I create a project. – SedJ601 Jul 14 '17 at 02:33
  • @James_D i have checked the location. And it is in same location in src/ folder – Tonmoy Mohajan Jul 14 '17 at 06:02
  • I found my problem! When i clean the controller and move all on-action from the scene builder i found that code runs! Here's is the screenshot link-http://imgur.com/SKr5Q1c – Tonmoy Mohajan Jul 14 '17 at 08:06
  • Here is some screenshot of scene builder- http://imgur.com/X1baD4J http://imgur.com/Wptw8bi – Tonmoy Mohajan Jul 14 '17 at 08:09

1 Answers1

0

I suggest to cange the root class in the main from Parent to AnchorPane:

public class Main extends Application {
  @Override  
  public void start(Stage stage) throws Exception {

    AnchorPane root = FXMLLoader.load(getClass().getResource("sample.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
 }

/**
* @param args the command line arguments
*/
  public static void main(String[] args) {
    launch(args);
  }  
}
Francesco
  • 396
  • 3
  • 7
  • 17