iCoder
iCoder

Reputation: 1534

Gradle JavaFX - java.lang.NullPointerException: Location is required

A null pointer exception is generated when the program is built using Gradle & executed via below command (Note: program works fine if I click run from within IntelliJ IDE):

java -jar main-1.0.jar

I tried the solution provided in Link which is similar to this case but it does not fix the issue.

I also tried the solution provided in Link but even that does not fix the issue.

The Gradle script incorporates solution from my earlier query - Link

The FXML file (Sample.fxml) is located in src/main/resources

Hope issue faced is clear & any pointers as to how this issue can be fixed ?

Code:

Main:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/Sample.fxml"));
        StackPane stackPane = new StackPane(root);
        Scene scene = new Scene(stackPane);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

Gradle:

plugins {
    id 'java'
    id 'application'
    id 'idea'
}

group 'testJavaFx'
version '1.0-SNAPSHOT'

sourceCompatibility = JavaVersion.VERSION_1_10

repositories {
    mavenCentral()
}

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/resources"]
            includes = ["**/*.fxml"]
        }
    }
}

dependencies {
    compile 'org.openjfx:javafx-base:11:win'
    compile 'org.openjfx:javafx-controls:11:win'
    compile 'org.openjfx:javafx-fxml:11:win'
    compile 'org.openjfx:javafx-graphics:11:win'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls',
                '--add-modules', 'javafx.fxml',
                '--add-modules', 'javafx.base',
                '--add-modules', 'javafx.graphics'
        ]
    }
}

run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls',
                '--add-modules', 'javafx.fxml',
                '--add-modules', 'javafx.base',
                '--add-modules', 'javafx.graphics'
        ]
    }
}

def jarPackage(String jarName, String className, artifactVersion) {
    if (artifactVersion == "" || artifactVersion == null) {
        artifactVersion = "1.0.0"
    }
    return tasks.create("jar${jarName}", Jar) {
        baseName = jarName
        version = artifactVersion

        String pkgName = className.substring(0, className.lastIndexOf("."))
        String pkgDir = pkgName.replaceAll("\\.", "/")
        String clazzName = className.substring( className.lastIndexOf(".") +1 )

        from(sourceSets.main.output) {
            include "$pkgDir//**"
        }

        from {
            configurations.compile.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        manifest {
            attributes "Implementation-Title": "$clazzName",
                    "Implementation-Version": "$version",
                    "Main-Class": "$pkgName.$clazzName"
        }
    }
}

artifacts {
    archives jarPackage("Main", "pkg1.Main" , "1.0")
}

Upvotes: 1

Views: 622

Answers (1)

iCoder
iCoder

Reputation: 1534

Managed to fix this by removing the SourceSets (below repositories) & adding the below code in tasks.Create

from(sourceSets.main.resources) {
            includes = ["**/*.*"]
}

Upvotes: 1

Related Questions