0

So I tried building a JavaFX application but I get a NullPointerException "Location is required". I already found a couple of questions regarding this but none of the answers worked for me.

I already tried:

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));

Parent root = FXMLLoader.load(getClass().getResource("/FXMLDocument.fxml"));

And moving FXMLDocument.fxml to default package but none of this worked for me. This is the untouched JavaFX FXML Application created by Netbeans, I really don't understand why this doesn't work out of the box...

Thanks in advance!

Project/File Structure:

enter image description here

enter image description here

Test.java:

package test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Tim
 */
public class Test 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);
    }

}

FXMLDocument.fxml:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

FXMLDocumentController.java:

package test;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
 *
 * @author Tim
 */
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
    }    

}

Stacktrace:

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:497)
    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:497)
    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$156(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 test.Test.start(Test.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(WinApplication.java:191)
    ... 1 more
Exception running application test.Test
Tim
  • 123
  • 1
  • 3
  • 13
  • You should be looking at what getResource returns, it's probably not anything sensible because your fxml is not where your .class file is, by the looks of it – pvg Dec 01 '15 at 19:43
  • Do you try to create from scratch your workspace in another project ? Because I copy/paste all your file and it seems to work with Netbean 8.0.2. – Aurelien Dec 01 '15 at 19:49
  • 3
    For some reason, the controller class has not been compiled, and the FXML file has not been deployed. Try cleaning and rebuilding the project. – James_D Dec 01 '15 at 19:54
  • I confirmed what @James_D said in my project I can see fxml and class files inside test/build/classes. – Aurelien Dec 01 '15 at 19:57
  • Thanks for the help so far, I tried debugging and it looks like getResource() returns null.. but I don't know why.. I created the project from scratch, it's the netbeans FXML Application template.. I tested it on my computer with Netbeans 8.0.2, 8.1 and on my laptop with 8.0.2 - the error stays the same – Tim Dec 01 '15 at 21:26

3 Answers3

1

@James_D was right, the project needed to be cleaned and rebuilt (Shift+F11) (even though I don't know why since I created the project from scratch via the Netbeans FXML Application template..)

Tim
  • 123
  • 1
  • 3
  • 13
0

If the method doesn't work at all you have to Clean and build your project. That's worked for me

K Scandrett
  • 16,390
  • 4
  • 40
  • 65
Henry
  • 19
  • 3
0

I had the same problem and could solve it. My answer is here JavaFX NullPointerException Location is required NetBeans . In netbeans 8.2 in the project properties under Build > Deployment there is a check-box "Request unrestricted access (Enable signing)".

toffi5
  • 1