Function loadShader throwing a NullPointerException for no reason

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 :

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 :

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 :

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

1 Like

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?

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

1 Like