无法访问RadioButtonGroup的空对象引用

时间:2012-09-18 07:32:05

标签: actionscript-3 flash

快速版:

当尝试将2个RadioButtonGroups与if语句进行比较时,如果其中一个组没有选择RadioButton,我会收到错误;

  

错误#1009:无法访问空对象引用的属性或方法。

长版:

我在actionscript中创建了2个列表,它们具有相同的内容,并将它们存储在RadioButtonGroups中。我们的想法是用户将从A列中选择一个元素,然后从B列中选择一个元素。此功能正常工作,但是当我执行验证时,如果两个列都被选中,程序在单击按钮时检查,我得到:

  

无法访问属性错误(参见上文)。

这是我的代码:

import fl.controls.RadioButtonGroup;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import fl.controls.RadioButton;

var focus1:RadioButtonGroup = new RadioButtonGroup("Focus 1");
var focus2:RadioButtonGroup = new RadioButtonGroup("Focus 2");
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
var btn1:Array = new Array();
var btn2:Array = new Array();

myLoader.load(new URLRequest("courseLoader.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
    myXML = new XML(e.target.data);

    for (var i = 0; i < myXML.COURSES.length(); i++) {
        var radA:RadioButton = new RadioButton();
        var radB:RadioButton = new RadioButton();

            // Create left focus column
        radA.x = 50;
        radA.y = i * 25 + 75;
        radA.width = 300;
        radA.name = "radA" + i;
        radA.label = myXML.COURSES[i].NAME[0];
        addChild(radA);
            btn1.push(radA);
        btn1[i].group = focus1;

        // Create right focus column
        radB. x = 450;
        radB.y = i * 25 + 75;
        radB.width = 300;
        radB.name = "radB" + i;
        radB.label = myXML.COURSES[i].NAME[0];
        addChild(radB);
        btn2.push(radB);
        btn2[i].group = focus2;
}

submit_btn.addEventListener(MouseEvent.CLICK, checkResult);

function checkResult(e:MouseEvent):void {
    var tempVar 

    if (focus1.selection.label == focus2.selection.label) {
        feedback.text = "Nope, they're both the same. Try again";
    } /* THIS IS WHERE IT STOPS WORKING */ else if (focus1.selection == null) {
        feedback.text = "You forgot to choose a focus from the 2nd column!";
    } else if (focus1.selection.label == null) {
        feedback.text = "You forgot to choose a focus from the 1st column!";
    } else if (focus2.selection.label == null) {
        feedback.text = "You forgot to choose a focuse from the 2nd column!";
    }
}

我尝试使用不同类型的属性和方法来比较两个组并检查其中一个组是否未被选中,但我一直收到同样的错误。

1 个答案:

答案 0 :(得分:0)

首先检查标签,然后检查选择是否为空。这是错误的,你必须按相反的顺序进行。

if (focus1.selection == null) {
    feedback.text = "You forgot to choose a focus from the 1st column!";
} else if (focus2.selection == null) {
    feedback.text = "You forgot to choose a focus from the 2nd column!";
} else if (focus1.selection.label == focus2.selection.label) {
    feedback.text = "Nope, they're both the same. Try again";
}