(resolved) Arrays of MaterialState

Hi,



Is it possible to have an array of MaterialStates? The reason I am asking is that I have many boxes which will have different individual colours associated with them. The problem that I am having is that when I use the following code, I get a NullPointer error.





private MaterialState ms[]; //This is an instance variable

ms = new MaterialState[num]; //Declared within the class’s constructor

//The following is declared in SimpleInit()

ms[counter] = display.getRenderer().createMaterialState();





I have the number of boxes declared with the num variable within the constructor. I don’t get a compilation error, but, I get a runtime error. Any ideas?

You’re allowed to have a array of RenderStates (MaterialState included) as long as you don’t try to apply them all to the same Spatial. You can only have a single instance of each type of state applied to an object (so one MaterialState, one TextureState, etc)



As for the NullPointerError you’re getting, unless you’re leaving some code out, those states are indeed null! You must create them from within the Renderer:



MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState()



Hope that helps!

sbook said:
You're allowed to have a array of RenderStates (MaterialState included) as long as you don't try to apply them all to the same Spatial. You can only have a single instance of each type of state applied to an object (so one MaterialState, one TextureState, etc)

As for the NullPointerError you're getting, unless you're leaving some code out, those states are indeed null! You must create them from within the Renderer:

`MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState()`

Hope that helps!


Thank you for the reply. I think I just worked out what the problem is. I think I need to do it when I attach the shapes to the root node. I can create the shapes before the simpleInitGame() method, however, I need to attach the shapes to the root node within the simpleInitGame(). I have a feeling that I also need to deal with the MaterialState within that method as well.

I am going to try it and see if that is the problem.

The problem was indeed where I was setting up the MaterialState. It should be done after the start() has been applied, so it must occur either during the InitGame(), otherwise, in the update.