2

I wanted to try JavaFX web browser deployment, so I started a very simple test project using the IntelliJ community edition 14.0.2 JavaFX Application template. Here's the code: (NO plugin, external library or maven)

Main.java

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}

Controller.java (attached to sample.fxml)

@FXML
private TextField textField;

@FXML
private Label label;

@FXML
private void setLabelText() {
    label.setText(textField.getText());
}

sample.fxml (Layout file)

<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
      prefHeight="400.0" prefWidth="600.0" spacing="20.0" xmlns="http://javafx.com/javafx/8"
      xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
   </padding>
   <TextField fx:id="textField" onAction="#setLabelText"/>
   <Label fx:id="label" text="Label">
      <font>
         <Font size="96.0"/>
      </font>
   </Label>
</VBox>

Now when I build the app as JAR, it runs without any problem. However, when I build it using the pre-defined "JavaFXApp" artifact, it generates one html, one jnlp and one jar. When I open up the html in my web browser (Firefox Nightly 64bit) It gives me the following error:

java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1838209255.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1604839423.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/982109627.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/1838209255.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$44/1604839423.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/982109627.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at sample.Main.start(Main.java:13)
    ... 11 more

The relevant line of code seems to be

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

However this same line of code works just fine when built and opened as single standalone JAR! Any idea on why the error occurs or how to fix it?

suushiemaniac
  • 47
  • 1
  • 6

1 Answers1

0

The error is somewhat cryptic, but understandable. you are getting a nullpointer with the localized message "Location is required", wich indicates that you did not set a location to find the FXML.

getClass().getResource("sample.fxml") returns null, and therefore the FXMLLoader throws a nullpointer. Check the path of your fxml, you might want to prepend it with a '/', like "/sample.fxml".

Maarten Blokker
  • 604
  • 1
  • 5
  • 23
  • That didn't fix the problem :( As I said in the original post, the standalone JAR works just fine, even wiithout prepending "/". It's just the JavaFX artifact that throws the error... – suushiemaniac Apr 22 '15 at 07:51
  • Then it looks like a classpath problem, you could examine the jar contents and see if sample.fxml is in it? – Maarten Blokker Apr 22 '15 at 08:08