java.lang.NullPointerException: Element.getRenderer(TextRenderer.class) returns nothing


here’s the code which throws me the error:


<?xml version=“1.0” encoding=“UTF-8”?>

<nifty xmlns=“” xmlns:xsi=“” xsi:schemaLocation=“”>

<useControls filename=“nifty-default-controls.xml” />

<useStyles filename=“nifty-default-styles.xml” />

<screen id=“start”>

<layer childLayout=“center”>

<control name=“label” text=“hello world” align=“center” valign=“center” />



<screen id=“hud_screen” name=“hud_screen” controller=“mygame.misc_classes.MyHUD”>

<layer id=“foreground” name=“layer_top” backgroundColor="#0000" childLayout=“horizontal” valign=“top”>

<panel name=“score_panel” id=“score_panel” width=“40%” height=“5%” childLayout=“horizontal” backgroundColor="#00f8">

<control name=“score_label” id=“score_label” text=“test” font=“Interface/Fonts/Impact.fnt” />






And in my inGameState’s update loop:


Element scorelbl = nifty.getScreen(“hud_screen”).findElementByName(“score_label”);

TextRenderer renderer = scorelbl.getRenderer(TextRenderer.class);



Did someone met similar issue?

No, score_label wasn’t a custom control. I thought that findElementByName() gets the name attribute value. In fact it gets the id… -.-

Thank you for your replies, it works now.

You’ve named your control “score_label”.

Have you created a custom control with this name and a TextRenderer?

Otherwise, change it to name=“label”.

You could also get the control:

[java]Element scorelbl = nifty.getScreen(“hud_screen”).findElementByName(“score_label”);

Label label = scorelbl.getNiftyControl(Label.class);



[xml]<screen id="start">[/xml]



does not mismatch

Also, what's score_label? Is it your custom control? I only know "label".