0

我创建了一个方法,该方法将清除以前对我的 ButtonGroup 所做的任何选择,该选择由单选按钮组成

    public void resetRadioButtons() {

    if (group.getSelection() != null) {
        group.clearSelection();
    }
    else {

    }

}

该组在此处实例化:

        final ButtonGroup group = new ButtonGroup();
        group.add(radioAnswer1);
        group.add(radioAnswer2);
        group.add(radioAnswer3);
        group.add(radioAnswer4);

调用该方法时,它会在该行给出 NullPointerException:

if (group.getSelection() != null) {

我不知道我做错了什么。提前致谢。

4

2 回答 2

1

很可能您正在隐藏group变量。尝试更换

final ButtonGroup group = new ButtonGroup();

group = new ButtonGroup();
于 2014-03-23T14:53:57.527 回答
0

If the exception is indeed on this line

if (group.getSelection() != null) {

(and not in getSelection() itself),
then group is null there.

Make sure it's initialized first.

于 2014-03-23T14:50:16.523 回答