0

I've been making a game and when it came to making the code much less messy i came across a null-pointer so i gave up and held Ctrl+z (undo) for a bit until it stopped. The code was back to normal but when i ran it, it comes up with the same null-pointer to do with a class that i didn't change what so ever. The null-pointer is as follows:

Starting programm...
Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at game.shaders.ShaderProgram.loadShader(ShaderProgram.java:98)
    at game.shaders.ShaderProgram.<init>(ShaderProgram.java:25)
    at game.shaders.StaticShader.<init>(StaticShader.java:34)
    at game.renderEngine.MasterRenderer.<init>(MasterRenderer.java:32)
    at game.engineTester.MainLoop.main(MainLoop.java:57)

The ShaderProgram seems to be causing the problem at line 98 were a bufferedreader is created and defined. The ShaderProgram lines that causes the error is as follows:

        try{
            InputStream in = Class.class.getResourceAsStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in)); // line 98, has nullpointer ???
            String line;
            while((line = reader.readLine())!=null){
                shaderSource.append(line).append("\n");
            }
            reader.close();
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }

i am using lwjgl for this the source for this DOES exist even though some people are complaining that it isnt. it is within the shaders package and it is vertexShader.txt and fragmentShader.txt

  • 1
    That simply means the resource `file` does not exist, and that getResourceAsStream thus returns null. http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String- – JB Nizet Jun 12 '16 at 06:27
  • Maybe #getResourceAsStream() returns null? This is easy solvable if you just debug your code. – n247s Jun 12 '16 at 06:27
  • the file certainly exists, it is a shader file .txt which is implemented and i havent changed it and it hasnt caused any problems before, im confused –  Jun 12 '16 at 06:39
  • 1
    If it did exist, you wouldn't get a NPE. The fact that you use the name `file` to store the path to a resource makes me think you don't really understand what a resource is and how they're located and loaded by getResourceAsStream. What is the value of file? What is your project layout and how do you run your program? Also, you shouldn't use Class.class.getResourceAsStream(), but YourOwnClass.class.getResourceAsStream(). – JB Nizet Jun 12 '16 at 06:50
  • There is nothing random about this. The resource doesn't exist, and you aren't checking for that case. – user207421 Jun 12 '16 at 06:55

0 Answers0