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:
[java]
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.
[/java]
I did a println on:
nifty
and
nifty.getScreen(“HUD”).findElementByName(“Layer_ID”).findElementByName(“tilePanel”).findElementByName(tileId)
. 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?
OK, did a little playing around, apparently
getRenderer(TextRenderer.class)
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.setText(“yourtexthere”);
//OR
Button button = nifty.getScreen(“HUD”).findElmentByName(“Layer_ID”).findControl(“tileId”,Button.class);
button.setText(“yourtexthere”);
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>)
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
[java]
Button button = screen.findNiftyControl(buttonId, Button.class);
[/java]
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’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.
Hi,
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)
b.setText("");
Using this I get a NullPointerException:
java.lang.NullPointerException
at de.lessvoid.nifty.controls.button.ButtonControl.setText(ButtonControl.java:124)
at tst.client.GameClient.updateInterface(GameClient.java:676)
I have no idea how to handle this, any suggestions?