1

快速版本:

尝试将 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!";
    }
}

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

4

1 回答 1

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";
} 
于 2012-09-18T08:02:06.877 回答