[Solved] TheForester crash on start

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

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

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

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:
[java]
Forester myForester = Forester.getInstance();
[/java]

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

1 Like