# Nifty TextRenderer object is null

Im new to nifty and Im trying to change text control using nifty in JME application following this tutorial.

Below is my TextRenderer code:

Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
TextRenderer textRenderer = element.getRenderer(TextRenderer.class);


Below is my nifty GUI code code for loadLevel scene:

<screen id="loadlevel" controller = "jmebase.TestLoadingScreen">
<panel id = "loadingpanel" childLayout="vertical" align="center" valign="center" height="32px" width="70%">
<control name="loadingbar" align="center" valign="center" width="100%" height="100%" />
text="                                                  "/>
</panel>
</layer>
</screen>


I printed value of for both element and textRenderer. element is initialised properly but textRenderer object is null. I googled lot and lots of people got this issue but none provided appropriate solution. Anyone know how to fix it. Im really stuck need help.

• A Nifty Control is a composition, consisting of multiple basic elements. The loadingtext element is actually not a text element but it has a nested text inside. You should get the Label Control from loadingtext. It has a setText method. Sorry, i'm on my phone. I can elaborate later. – 1000ml Apr 19 '16 at 3:17
• loadingtext element is label element and doesnt have setText method. so we get the TextRenderer wrapper for it which has setText method. But I dont know why it is null. Please post your elabotared answer. – Crawler Apr 19 '16 at 6:24
• For the case of a Label my comment was wrong. Because a Label only consists of a single text element, the control tag effectively gets replaced by this text element. – 1000ml Apr 19 '16 at 13:12

getRenderer is going to return null if the element does not contain the class you're trying to retrieve. In this case, the element doesn't contain a TextRenderer, so it returns null.

You can avoid the error:

Element element = nifty.getScreen("loadlevel").findElementByName("loadingtext");
if(element == null)
return;

TextRenderer textRenderer = element.getRenderer(TextRenderer.class);
if(textRenderer == null)
return;



To get your expected result of setting the text, you need to ensure the element you're locating is properly configured.

• element is properly initialised but textRenderer is still null. all code is from above tutorial. going through jme forum i found other dev also have same problem. I tried their solutions but it didnt work. Any ideas where I went wrong? – Crawler Apr 18 '16 at 5:44
• Can't say for sure. The textRenderer isn't being initialized. Attach the debugger and step through the code. See where things are going wrong. – MichaelHouse Apr 18 '16 at 18:42

First off, the tutorial you linked is outdated. I also can't find any reference to Nifty in it.

I'm using Nifty 1.4 and cannot reproduce your problem. I can set the text via TextRenderer from the label.

However, you should avoid using renderers directly if you can achieve the same with a NiftyControl:

Element loadingtext = screen.findElementById("loadingtext");