# Function loadShader throwing a NullPointerException for no reason

**URL:** <https://discourse.processing.org/t/function-loadshader-throwing-a-nullpointerexception-for-no-reason/20038>\
**Category:** Coding Questions\
**Created:** [April 22, 2020, 5:21pm UTC](https://discourse.processing.org/t/function-loadshader-throwing-a-nullpointerexception-for-no-reason/20038 "2020-04-22T17:21:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ayfri](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ayfri/32/8797_2.png) [@Ayfri](https://discourse.processing.org/u/Ayfri)\
**Post date:** [April 22, 2020, 5:21pm UTC](https://discourse.processing.org/t/function-loadshader-throwing-a-nullpointerexception-for-no-reason/20038/1 "2020-04-22T17:21:55Z")

</div>

Hi, I’m trying to make shaders in my Program, I have this code that loads a simple vertex Shader from his name to find it in the `resources` folder :

```auto
public void init() throws FileNotFoundException {
	try {
		Registry.register(getShader("fragment"), "basic");
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public PShader getShader(String fragName) throws FileNotFoundException {
	String path = "src\\main\\resources\\shaders\\" + fragName + ".glsl";
	File file = new File(path);
	
	if (file.exists()) {
		Logger.info("Fragment Shader " + file.getAbsolutePath() + " found.", "ShaderMapping");
		return sketch.loadShader(file.getAbsolutePath());
	} else {
		throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found.");
	}
}

```

It register it into my Registry, all this happens in the `setting` function.  
In the `draw` function at a point I do this :

```auto
Registry.getShaders().forEach((name, shader) -> {
	sketch.shader(shader);
});

```

To draw them.

**BUT**

In my loading part (in my `setting` function), where it loads the shader, I get this error :

```auto
java.lang.NullPointerException
	at processing.core.PApplet.loadShader(PApplet.java:11729)
	at main.java.fr.ayfri.Minecraft.utils.ShaderManager.getShader(ShaderManager.java:30)
	at main.java.fr.ayfri.Minecraft.utils.ShaderManager.init(ShaderManager.java:18)
	at main.java.fr.ayfri.Minecraft.Minecraft.<init>(Minecraft.java:28)
	at main.java.fr.ayfri.Minecraft.Main.settings(Main.java:27)
	at processing.core.PApplet.handleSettings(PApplet.java:978)
	at processing.core.PApplet.runSketch(PApplet.java:10897)
	at main.java.fr.ayfri.Minecraft.Main.main(Main.java:16)

```

What I think that is oddly is that the Logger says it FINDS the shaders, without errors, it’s just where it load these that it send the error.  
_Look at my screen for proof._ [http://prntscr.com/s44ti5](http://prntscr.com/s44ti5)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 3, 2020, 6:30pm UTC](https://discourse.processing.org/t/function-loadshader-throwing-a-nullpointerexception-for-no-reason/20038/2 "2020-05-03T18:30:59Z")

</div>

> [@Ayfri](#):
>
> ```auto
> Registry.getShaders().forEach((name, shader) -> {
> 
> ```

Were you able to resolve this issue?

Are you working in eclipse / IntelliJ (not PDE) and importing processing as a library to a Java sketch? I ask because `->` is not supported by Processing 3 syntax, as used in PDE.

If Java, what version of Java are you using?

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [May 6, 2020, 7:04am UTC](https://discourse.processing.org/t/function-loadshader-throwing-a-nullpointerexception-for-no-reason/20038/3 "2020-05-06T07:04:41Z")

</div>

Is it possible that it’s trying to enable the shader before the P2D / P3D is set up and running?
