Radiobutton group problem

Hey everyone,



I came accross a problem with radio buttons group.

I have defined a popup in my gui.xml:



[xml]

<nifty>



<popup id=“popup-select-reward” style=“popup-select-reward-style”>

<panel id=“select-reward-panel” style="#panel">

<control id=“rewards” name=“radioButtonGroup” style="#rewards"/>

<control id=“select-reward-button” name=“button” style=“button-style”/>

</panel>

</popup>



</nifty>

[/xml]



When I decide that the popup must appear I use java to create popup and fill it with radio buttons.

[java]

Element popup = nifty.createPopup(“popup-select-reward”);

RadioButtonGroup rbg = popup.findNiftyControl(“rewards”, RadioButtonGroup.class);



for(int i=0;i<5;++i) {

RadioButtonBuilder rbb = new RadioButtonBuilder(“rbid” + i);

rbb.group(rbg.getId());

rbb.style(“radiobutton-style”);

rbb.build(nifty, nifty.getCurrentScreen(), rbg.getElement());//HERE i GET NullPointerException DURING LINKING TO GROUP

}

nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);

[/java]



The problem appears in the line with the comment.

I get this:

WARNING: missing element/control with id [rewards] for requested control class [de.lessvoid.nifty.controls.radiobutton.RadioButtonGroupControl]

Exception in thread “Thread-8” java.lang.NullPointerException

at de.lessvoid.nifty.controls.radiobutton.RadioButtonControl.linkToRadioGroup(RadioButtonControl.java:111)

at de.lessvoid.nifty.controls.radiobutton.RadioButtonControl.bind(RadioButtonControl.java:32)

at de.lessvoid.nifty.controls.NiftyInputControl.bindControl(NiftyInputControl.java:121)

at de.lessvoid.nifty.elements.Element.bindControls(Element.java:2017)

at de.lessvoid.nifty.Nifty.createElementFromTypeInternal(Nifty.java:1434)

at de.lessvoid.nifty.Nifty.createElementFromType(Nifty.java:1424)

at de.lessvoid.nifty.builder.ElementBuilder.build(ElementBuilder.java:448)



The problem occurs because the method ‘linkToRadioGroup’ tries to find the group nifty control on the screen.



[java]

private void linkToRadioGroup(final String groupId) {

if (groupId == null) {

radioGroup = null;

return;

}

radioGroup = screen.findNiftyControl(groupId, RadioButtonGroupControl.class);

radioGroup.registerRadioButton(this);

}

[/java]



But it is defined within a popup.

The second line of my code:

[java]

RadioButtonGroup rbg = popup.findNiftyControl(“rewards”, RadioButtonGroup.class);

[/java]

works fine and finds the group control.



Is there any workaround for this or am I supposed to define the control on the screen ??







Cheers,

Kaelthas

1 Like

Yes, unfortunately this won’t work currently :confused: The problem is that the linkToRadioGroup() method tries to find the radioButtonGroupControl on the current screen but since the popup is not yet displayed (showPopup() is not called yet) this element can’t be found.



I’m not sure how Nifty could solve that in a general way. Maybe show the popup first and add the RadioButton when it is displayed? Still somewhat ugly tho :confused:

1 Like

Can the radio buttons detect when they are first displayed and do the linking then?

it’s so bad that radiobutton does not work with popup menu. :frowning:
I had to use a list instead.

A year and a half later, do you guys know if this is resolved yet? I’m getting the same error. If not, then has anybody found a way to make radio buttons work on nifty popups?

EDIT: OMG nevermind. I copied an example that had a typo in it causing the exception. It does work perfectly indeed!

@.Ben. said: A year and a half later, do you guys know if this is resolved yet? I'm getting the same error. If not, then has anybody found a way to make radio buttons work on nifty popups?

EDIT: OMG nevermind. I copied an example that had a typo in it causing the exception. It does work perfectly indeed!

I can promise you this is not resolved in Nifty. I have been dealing with the same error in the latest Nifty 1.4 snapshot. I’m sure JME has some sort of workaround, which is probably why it works for you.