1

I'm new to JavaFX and I was trying to make a JavaFX FXML Application project using Netbeans IDE but I face this exception.

This my java class:

    public class JavaFXApplication9 extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.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);
        }

    }

This My FXML Code:

    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.AnchorPane?>

    <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="javafxapplication9.FXMLDocumentController">
        <children>
            <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        </children>
    </AnchorPane>

This is my controller class :

    public class FXMLDocumentController implements Initializable {

        @FXML
        private Label label;

        @FXML
        private void handleButtonAction(ActionEvent event) {
            System.out.println("You clicked me!");
            label.setText("Hello World!");
        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    

    }

That is Exception in Application start method java.lang.reflect.InvocationTargetException javafx

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 javafxapplication9.JavaFXApplication9.start(JavaFXApplication9.java:22)
    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 javafxapplication9.JavaFXApplication9
    E:\Netbeans Project\JavaFXApplication9\nbproject\build-impl.xml:1052: The following error occurred 
    while executing this line:
    E:\Netbeans Project\JavaFXApplication9\nbproject\build-impl.xml:806: Java returned: 1
    BUILD FAILED (total time: 1 second)
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • The code you posted works for me. Is class `FXMLDocumentController` in package `javafxapplication9` ? Is file `FXMLDocument.fxml` located in the same directory as file `JavaFXApplication9.class` ? Note I am asking about the compiled class and **not** the source code file. – Abra Jun 05 '20 at 05:30
  • in case your project structure is ok, you may also need to check [answers to this question](https://stackoverflow.com/questions/20507591/javafx-location-is-required-even-though-it-is-in-the-same-package) – Nowhere Man Jun 05 '20 at 05:35
  • The stack trace show this location : Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); – Mirwais Faizi Jun 05 '20 at 05:43

1 Answers1

-1

The main cause of this exception is a NullPointerException (see stacktrace: Location is required ). So you try to use something which is not initiated. Hard to find a mistake without any code snippets.

André Schmidt
  • 89
  • 1
  • 12
  • The stack trace show this location : Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); – Mirwais Faizi Jun 05 '20 at 05:43
  • @MirwaisFaizi make sure that your FXMLDocument.fxml is in the same folder as the JavaFXApplication9 class. The getClass() method, for all intents and purposes, gets the location of the class from which its called, and the getResource() method then finds the resource with respect to that. So getClass().getResource("FXMLDocument.fxml") points to a file that is in the same folder. – matt_frangakis Jun 05 '20 at 08:41
  • My Java class, controller class, and FXML class in the same folder. – Mirwais Faizi Jun 05 '20 at 09:27