Quick version:
When trying to compare 2 RadioButtonGroups with an if statement, if one of the groups does not have a RadioButton selected, I get an error;
Error #1009: Cannot access a property or method of a null object reference.
Long version:
I am creating 2 lists in actionscript that have the same content and have stored them in RadioButtonGroups. The idea is that a user will choose an element from column A and then select an element from column B. This functionality works fine, but when I do validation where the program checks when clicking a button if both columns have been selected, I get:
Cannot access property error (See above).
Here is my code:
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!";
}
}
I’ve tried using different kinds of properties and methods for comparing both groups and checking if one of them is not selected but I keep getting the same error.
You first are checking for labels, and only THEN you check if selection is null. This is wrong, you have to do it in reverse order.