TypeError: Error #1009
Hello,
For a quiz I am building an application.
I am almost done, but I keep getting the following error:
TypeError: Error #1009: Kan geen eigenschap of methode benaderen via een verwijzing naar een object dat null is.
at QuizApp_fla::MainTimeline/generateAnswers()
at QuizApp_fla::MainTimeline/generateQuestion()
at QuizApp_fla::MainTimeline/LoadXML()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Below is my code, I am hoping someone can help me..:
//import flash.external.ExternalInterface;
import fl.controls.RadioButton;
import fl.controls.RadioButtonGroup;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var xml:XML;
var xmlList:XMLList;
var xmlChildren;
var totalQuestions:Number;
var totalAnswers: Number;
var radioButton:RadioButton;//a radio button
var radioButtonGroup:RadioButtonGroup;
var vPadding:uint = 0;//control the distance between radio buttons
var vSpacing:uint = 50;//provide equal space between radio buttons
var isCorrect:Number;
var correctAnswerIndex:Number;
var radioButtonArray:Array;
var radioButtonContainer:MovieClip = new MovieClip();
nextButton.visible = false;
var currentRadioButton;
var currentSelection;
var resultStr:String;
var result_TF:TextField = new TextField (); //to display result for current question
var currentQuestion:Number = 0;
//ExternalInterface.addCallback("nextQuestion", null, nextQuestion);
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("quiz.xml"));
addChild(radioButtonContainer);
function LoadXML(e:Event):void {
xml = new XML(xmlLoader.data); //create XML document
xmlList = new XMLList(xml); //create XML List of XML nodes
xmlChildren = xmlList.children (); //Get number of child nodes
generateQuestion (); // This function extracts question from XML
}
function generateAnswers (): void {
totalAnswers = xmlChildren[currentQuestion].answer.length();
radioButtonGroup = new RadioButtonGroup("answers");
for (var i:int = 0; i < totalAnswers; i++) {
radioButton = new RadioButton(); // create radio button
radioButton.selected = false;
//Previous addChild(radioButton) statement is replaced by following
radioButtonContainer.addChild(radioButton);
addChild(radioButton); //add it to the stage
radioButton.x = Question_TF.x + 30; // adjust ‘x’ position
radioButton.y = (vPadding + vSpacing) + (Question_TF.y + Question_TF.height);
vPadding += vSpacing;
radioButton.label = xmlChildren[currentQuestion].answer;
radioButton.width = 300; // for proper display of options
radioButton.group = radioButtonGroup; //grouping radio buttons
isCorrect = xmlChildren[currentQuestion].answer.@correct;
if(isCorrect == 1){
correctAnswerIndex = i; //this is the index no. of correct option
}
}
radioButton.addEventListener(MouseEvent.CLICK, checkAnswer);
//push radio buttons into array for reference to removeChild()
radioButtonArray.push(radioButton);
}
function generateQuestion (): void {
Question_TF.text = xmlChildren[currentQuestion].questionText;
totalQuestions = xmlChildren.length();
generateAnswers();
radioButtonArray= new Array();
}
function checkAnswer(e:MouseEvent):void {
currentRadioButton = e.currentTarget; // store selected radio button
currentSelection = radioButtonGroup.getRadioButtonIndex(currentRadioButton)();
resultStr = (currentSelection == correctAnswerIndex) ? "Correct" : "Incorrect";
result_TF.text = resultStr;
result_TF.x = radioButton.x + 30; //’x’ position
result_TF.y = (vPadding + vSpacing)+50; //’y’ position
if(resultStr == "Correct"){
//check if current question is last question
if(currentQuestion < totalQuestions-1){
nextButton.visible = true;
nextButton.addEventListener(MouseEvent.CLICK, nextQuestion);
//next button position
nextButton.x = result_TF.x + result_TF.width + 40;
nextButton.y = result_TF.y;
}else{
result_TF.text = resultStr + " —> Quiz Complete";
}
}else
{
nextButton.visible = false;
nextButton.removeEventListener(MouseEvent.CLICK, nextQuestion);
}
}
result_TF.width = 250; //for proper text display
addChild (result_TF); //add it to the stage
//——– Go to next question ————————–
function nextQuestion(e:MouseEvent):void{
Question_TF.text = "";
result_TF.text = "";
for (var i:int = 0; i < totalAnswers; i++){
radioButtonContainer.removeChild(radioButtonArray);
}
if(currentQuestion < totalQuestions-1){
currentQuestion++;
vPadding = 0;
totalAnswers = 0;
radioButtonArray = [];
generateQuestion();
}
}
Thanks!
