0

Hi sadly i just dont get why i receive a nullpointer:

My ResourceLoader class

public static String loadResource(String path){
       StringBuilder result = new StringBuilder();
       try {
           InputStreamReader isr = new InputStreamReader(Class.class.getResourceAsStream(path)) ;
           BufferedReader reader = new BufferedReader(isr);
           String line = "";
           while((line = reader.readLine()) != null){
                result.append(line).append("\n");
           }
       }catch (IOException e){
           System.out.println("File nicht gefunden:  " + e);
       }
    return result.toString();
    }

This is where i use it

shaderProgram.createVertexShader(ResourceLoader.loadResource("shaders/mainVertex.glsl"));
shaderProgram.createFragmentShader(ResourceLoader.loadResource("shaders/mainFragment.glsl"));

This is the Exeption i receive

java.lang.NullPointerException
    at java.base/java.io.Reader.<init>(Reader.java:167)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at utils.ResourceLoader.loadResource(ResourceLoader.java:13)
    at graphics.Renderer.init(Renderer.java:32)
    at GameEngine.init(GameEngine.java:43)
    at GameEngine.run(GameEngine.java:33)
    at Main.main(Main.java:9)

Process finished with exit code 0

Thank your for looking at it!

2
  • From the exception stack trace, it looks like Class.class.getResourceAsStream(path) is returning null. Commented May 24, 2020 at 8:46
  • This question seems to be a duplicate of "NPE from class getResource" Commented May 24, 2020 at 8:48

1 Answer 1

1

As NomadMaker pointed out, the issue lies with the call

new InputStreamReader(Class.class.getResourceAsStream(path))

returning a null value (you would have seen this as part of the error message).

This should work for you:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;

public class Main {

    public static void main(String[] args) {
        String filepath = "test.txt";
        System.out.println(loadResource(filepath));
    }

    public static String loadResource(String path) {
        InputStream is = Main.class.getClassLoader().getResourceAsStream(path);
        StringBuilder result = new StringBuilder();
        try (InputStreamReader isr = new InputStreamReader(Objects.requireNonNull(is));
             BufferedReader reader = new BufferedReader(isr)) {
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }
        } catch (IOException | NullPointerException e) {
            System.out.println("File nicht gefunden:  " + e);
        }
        return result.toString();
    }
}

Issues to keep in mind:

  1. You need to close the InputStreamReader and BufferedReader when you are done with these. I used a try-with-resources here (refer to the official documentation if this is new to you).

  2. This code assumes that you stored the file you are reading from inside the resources folder. If the file can't be found, a NPE error will be thrown, which I am including in the catch. You may choose to deal with the exception in a different way.

  3. This code also assumes that you are working within a static context. If not, use this.class.getClass()getClassLoader().getResourceAsStream(path) instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding getClassLoader() solved my problem. Thanks Buddy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.