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

Hi,



here’s the code which throws me the error:



[xml]

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

<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>

<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” />

</layer>

</screen>

<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” />

</panel>

</layer>

</screen>

</nifty>

[/xml]



And in my inGameState’s update loop:



[java]

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

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

renderer.setText(“testtest”);

[/java]



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);

label.setText(“testtest”);[/java]

Well:


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


and

nifty.getScreen("hud_screen"


does not mismatch

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