0

I have a simple program that reads a text file (test.txt) line by line and prints each line to the console. In intellij it works just fine.


import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;

public class testing {

    public static void main(String[] args) {
        testing main= new testing();
        main.handleData("test.txt");
        //    handleData();
        //System.out.println("hello world");

    }


    public void handleData(String fileName)  {
        System.out.println("Testing");

        File file= new File(getClass().getResource(fileName).getPath());

        try {
            Scanner scanner = new Scanner(file);
            while(scanner.hasNextLine()){
                System.out.println(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

}

I am trying to build it with gradle and when i run the jar command java -jar out/artifacts/helloTestingWorld_jar/helloTestingWorld.jar I get an error saying the path is null

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.getPath()" because the return value of "java.lang.Class.getResource(String)" is null
        at testing.handleData(testing.java:22)
        at testing.main(testing.java:12)

My build.gradle file looks like this

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

}
jar {
    manifest {
        attributes "Main-Class": "src.main.java.testing"
    }

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
test {
    useJUnitPlatform()
}

My resource folder is marked as the resource root and my java folder that contains my main class is marked as the source root. I am thinking that I might have to add the text file as a dependency in the jar file?

I have had a look at all of the other suggestions on here and the all lead to the same result. I have tried rebuilding the project from scratch and still the same result.

I have also tried using InputStream instead of File

 InputStream in = getClass().getResourceAsStream(fileName);

When I use InputStream I get this error

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.io.Reader.<init>(Reader.java:168)
        at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:76)
        at java.base/java.util.Scanner.<init>(Scanner.java:566)
        at testing.handleData(test.java:23)
        at testing.main(test.java:10)
Jane
  • 17
  • 5
  • Does this answer your question? [Avoiding NullPointerException in Java](https://stackoverflow.com/questions/271526/avoiding-nullpointerexception-in-java) – Martin Zeitler Feb 10 '22 at 12:30
  • Does this answer your question? [Loading files from Resource folder using java jar](https://stackoverflow.com/questions/47917335/loading-files-from-resource-folder-using-java-jar) – pringi Feb 10 '22 at 12:35
  • You are not showing your jar file. It looks more like the build tool configuration file. – Queeg Feb 10 '22 at 13:34

0 Answers0