Nifty change button text through Java

I’m back again!

This time I’m trying to change text and functionality of a nifty button through Java. Through a stack error, it is giving me a null pointer error and fails to change the text… This is the line of code i’m using to change the text:
nifty.getScreen(“HUD”).findElementByName(“Layer_ID”).findElementByName(“tilePanel”).findElementByName(tileId).getRenderer(TextRenderer.class).setText(“Create Unit”);
//it should be noted that tileId is passed to a function, which includes this line of code. My null reference points to this line of code.

I did a println on:
. Both returned valid values, so the problem should theoretically be here:
getRenderer(TextRenderer.class).setText(“Create Unit”);
, but I don’t know why this would return null. Any ideas?

One dot per line of code is my rule. Makes it easy to see which value is null.

OK, did a little playing around, apparently
is null. Any ideas why that would be? I don’t have to explicitly assign a “text renderer” to every button, do I?

When you are using controls you shoudn’t use low api such text renderer . Indeed you are taking the renderer of the element wich cointains the control , in this case ButtonControl . You should always access to control trough the getControl or findControl methods . In your case :
[java]Element tileid = nifty.getScreen(“HUD”).findElementByName(“Layer_ID”).findElementByName(“tilePanel”).findElementByName(tileId);
Button button = tileid.getControl(Button.class);
Button button = nifty.getScreen(“HUD”).findElmentByName(“Layer_ID”).findControl(“tileId”,Button.class);


1 Like

OK, I’m trying that, but it doesn’t seem to like “Button.class”.

Button button = nifty.getScreen(“HUD”).findElementByName(“Layer_ID”).findElementByName(“tilePanel”).findControl(tileId, Button.class);

The compiler says this:

method findControl in class Element cannot be applied to given types;
required: String,Class<T>
found: String,Class<Button>
reason: inferred type does not conform to declared bound(s)
inferred: Button
bound(s): Controller
where T is a type-variable:
T extends Controller declared in method <T>findControl(String,Class<T>)

Which is a bit gibberish-y to me. Any ideas?

The error is because findControl()'s second argument should be a subclass of de.lessvoid.nifty.controls.Controller whereas Button is a subclass of de.lessvoid.nifty.controls.NiftyControl.

You can see the class hierarchies more easily if you download the JavaDoc:

To find a particular button, I would simply assign it an id in my XML and then do
Button button = screen.findNiftyControl(buttonId, Button.class);

1 Like

OK, thanks, that worked perfectly.
Now I just have to change it’s interaction on click. (Just FYI, I’m not using any XML atm. Just Java, if I can get away with it.)

I assume that the instructions in the tutorial ( are going to flop for roughly the same reason as changing text. I can’t seem to find any method that will change the interaction in my button object. How do I do this?

I’m sure it can be done. But it might be simpler to have a single handler method (for each button, perhaps) which does different things depending on the context.

1 Like

Huh… now that’s a thought. I’l give it a try.

By the way sorry for my aswer ! :smiley: I aswered by heart without test if the code worked !:facepalm: . I’m glad to see that you solve that anyway!


sorry for grabbing this old thread but it perfectly fits to my problem so I think it is useless to start a new one.

I am trying to change a button’s label like described above:

Button b = game.getNiftyDisplay().getNifty().getScreen("buildingInfo").findNiftyControl("buttonInventory", Button.class);
                        if(b != null)

Using this I get a NullPointerException:

	at de.lessvoid.nifty.controls.button.ButtonControl.setText(
	at tst.client.GameClient.updateInterface(

I have no idea how to handle this, any suggestions?

Thanks in advance :slight_smile: