0

Project Structure

/spp-test-automation
  /src
    /main
    /test
      /java
        /com.testing.tool
          Processor.java
      /resources
        processor.json

Within Processor.java I am trying to grab the contents in my processor.json file. However, I am getting a NullPointerException and I am not sure why.

For the variable filepath, I've tried the values "src/test/resources/procesoosr.json", "processor.json", "\processor.json" but I am still getting a NullPointerException.

BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath));

I have even passing in the absolute path but I get the same error as well.

File file = new File("src/test/resources/processor.json");
String absolutePath = file.getAbsolutePath();

I am using Intellij to run these tests and this is what my config looks like. Ignore the red text and I simply renamed the package but the concept is essentially the same

enter image description here

Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • 3
    Does this answer your question? [Preferred way of loading resources in Java](https://stackoverflow.com/questions/3861989/preferred-way-of-loading-resources-in-java) – Amongalen Jan 22 '20 at 10:23
  • What have you tried to spot the error? Usually, with current IDEs, you can use a pretty good debugger – Nico Haase Jan 22 '20 at 13:23

3 Answers3

1

Try adding a dot before src to to indicate that you are using the relative path to the project root directory. Otherwise you will search by absolute path.

File file = new File("./src/test/resources/processor.json");
String absolutePath = file.getAbsolutePath();
Maffe
  • 430
  • 6
  • 14
0

Use

    ClassLoader classLoader = new ReadResourceFileDemo().getClass().getClassLoader();
    File file = new File(classLoader.getResource(fileName).getFile());

where fileName="processor.json"

AbrA
  • 434
  • 1
  • 6
  • 19
0

to start: right click on the file : choose Copy path: there are 3 paths:

  1. Absolute path
  2. Content root path
  3. Source root path

usually is content root path. After test with those paths

BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
Rom
  • 67
  • 1
  • 2
  • 12