[Solved] TheForester crash on start

I’m trying to add foliage to my game using TheForester plugin. Here’s the code:

forester = new Forester();
forester.initialize(rootNode, cam, terrainTest, this);
GrassLoader gl = forester.createGrassLoader(65, 2, 100.0F, 10.0F);
gl.addLayer(assetManager.loadMaterial(“Materials/GrassTest.j3m”), GrassLayer.MeshType.QUADS);

When I run it, it crashes on start with a NullPointerException in the creation of the GrassLayer. Here’s the line that’s failing:
material = new SPLightMaterial(Forester.getInstance().getApp().getAssetManager(),mat.getMaterialDef().getAssetName());

Upon some of my own investigation, it appears "Forester.getInstance().getApp() returns null. In my class, if I do “Forester.getInstance().getApp()” it returns null, but if I directly reference the instance of forester in my class, it works fine. It’s like it’s returning two different instances of Forester. Is there something I’m missing here?

EDIT: I fixed the problem after looking at the code for “getInstance()”. If anyone else is having this problem, the fix is to never call “= new Forester();” Use this instead:
Forester myForester = Forester.getInstance();

If Forester is a Singleton then the constructor really should have been made private (not your fault, just saying)

1 Like