10

I'm getting

java.lang.NullPointerException: Location is required.

when I run my program after assembling using gradle with javafx plugin. If I run it from IntelliJ Idea, everything is all right. The Java source files and .fxml are locate in some package.

build.gradle

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'idea'
apply from: 'javafx.plugin'

javafx {
    javaRuntime = 'C:\\Program Files\\Java\\jdk1.7.0_45'

    appID 'FXMLExample'
    appName 'fxml example application'
    mainClass 'local.hz.FXMLExample'
}

task "create-dirs" {
    sourceSets*.java.srcDirs*.each {it.mkdirs()}
    sourceSets*.resources.srcDirs*.each {it.mkdirs()}
}

fxml_example.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane fx:controller="local.hz.FXMLExampleController"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
<gridLinesVisible>true</gridLinesVisible>

<Text text="Welcome"
      GridPane.columnIndex="0" GridPane.rowIndex="0"
      GridPane.columnSpan="2"/>

<Label text="User Name:"
      GridPane.columnIndex="0" GridPane.rowIndex="1"/>

<TextField
      GridPane.columnIndex="1" GridPane.rowIndex="1"/>

<Label text="Password:"
      GridPane.columnIndex="0" GridPane.rowIndex="2"/>

<PasswordField fx:id="passwordField"
               GridPane.columnIndex="1" GridPane.rowIndex="2"/>

<HBox spacing="10" alignment="bottom_right"
      GridPane.columnIndex="1" GridPane.rowIndex="4">
    <Button text="Sign In"
            onAction="#handleSubmitButtonAction"/>
</HBox>

<Text fx:id="actiontarget"
      GridPane.columnIndex="1" GridPane.rowIndex="6"/>
</GridPane>

FXMLExample.java

package local.hz;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class FXMLExample extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));

    Scene scene = new Scene(root,300,275);

    stage.setTitle("FXML WELCOME!!");
    stage.setScene(scene);
    stage.show();
}

public static  void main(String[] arguments) {
    launch(arguments);
}
}
jerry
  • 2,581
  • 1
  • 21
  • 32
Hugo Stiglits
  • 101
  • 1
  • 3

4 Answers4

29

Your FXML files are probably not packaged into your jar.

Try adding this to your build.gradle file:

sourceSets {
  main {
    resources {
        srcDirs = ["src/main/java"]
        includes = ["**/*.fxml"]
    }
  }
}
Phil C
  • 431
  • 4
  • 7
  • 3
    This answer is correct. Author of the question could accept it. – Konrad G Jan 20 '17 at 08:45
  • Thanks @KonradG. Worked for me. – Phil C Jan 22 '17 at 19:12
  • If you put `*.fxml` under the packages which have your code, you shoud write `srcDirs = ["path/to/your/code/root/"]` – Yushan ZHANG Dec 25 '17 at 11:12
  • I am just the other one that this response save my day and help me to run my JavaFX app with gradle correctly.Thank you ! I do not have a external resource directory in my project. Just a "views" directory in a same root of Main class app. And I load my main fxml like that getResource("views/main.fxml")); – miltone Sep 26 '20 at 13:12
4

I'm really late to the party...but the accepted didn't work. I (randomly) added 'javafx.fxml' to the list of modules, like this

javafx {
    version = "14"
    modules = [ 'javafx.controls', 'javafx.fxml']
}

...and it solved the issue.

MelS
  • 79
  • 2
1

Actually, loading a resource with Gradle might be different from using your IDE resource system.

It is better to use the ClassLoader :

URL myFxmlURL = ClassLoader.getSystemResource("fxml_example.fxml");
FXMLLoader loader = new FXMLLoader(sampleUrl);

Parent root = loader.load(myFxmlURL);
-1

Try replacing:

javaRuntime = 'C:\\Program Files\\Java\\jdk1.7.0_45'

with:

javaRuntime = 'C:/Program Files/Java/jdk1.7.0_45'
akhikhl
  • 2,552
  • 18
  • 23