JaEmiX
JaEmiX

Reputation: 1

NULL Pointer at InputStreamReader ClassPath

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!

Upvotes: 0

Views: 1536

Answers (1)

cadebe
cadebe

Reputation: 691

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.

Upvotes: 1

Related Questions