Skip to main content
January 3, 2013
Answered

TypeError: Error #1009

  • January 3, 2013
  • 2 replies
  • 1687 views

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!

This topic has been closed for replies.
Correct answer esdebon

Perfect !!

radioButtonArray is null

try

function generateQuestion (): void {

          Question_TF.text = xmlChildren[currentQuestion].questionText;

          totalQuestions = xmlChildren.length();

          radioButtonArray= new Array();  // <---------------- First

          generateAnswers();                      // <---------------- Second

     }

2 replies

January 3, 2013

Thanks very much!

esdebon
Inspiring
January 3, 2013

you're welcome

esdebon
Inspiring
January 3, 2013

You have a not defined object

Try using trace and tell me the last trace after run it

function generateAnswers (): void {

     trace(0);

          totalAnswers = xmlChildren[currentQuestion].answer.length();

    trace(1);

          radioButtonGroup = new RadioButtonGroup("answers");

    trace(3);

          for (var i:int = 0; i < totalAnswers; i++) {

    trace(i,0);

                    radioButton = new RadioButton(); // create radio button

                    radioButton.selected = false;

                    //Previous addChild(radioButton) statement is replaced by following

trace(i,1);

                    radioButtonContainer.addChild(radioButton);

                    addChild(radioButton); //add it to the stage

trace(i,2);

                    radioButton.x = Question_TF.x + 30; // adjust ‘x’ position

trace(i,3);

                    radioButton.y = (vPadding + vSpacing) + (Question_TF.y + Question_TF.height);

trace(i,4);

                    vPadding += vSpacing;

trace(i,5);

                    radioButton.label = xmlChildren[currentQuestion].answer;

trace(i,6);

                    radioButton.width = 300; // for proper display of options

trace(i,7);

                    radioButton.group = radioButtonGroup; //grouping radio buttons

trace(i,8);

                    isCorrect = xmlChildren[currentQuestion].answer.@correct;

trace(i,9);

                    if(isCorrect == 1){

                             correctAnswerIndex = i; //this is the index no. of correct option

trace("correct");

                              }

                    }

          radioButton.addEventListener(MouseEvent.CLICK, checkAnswer);

trace(4);

          //push radio buttons into array for reference to removeChild()

          radioButtonArray.push(radioButton);

trace(5);

     }

Inspiring
January 3, 2013

If you go into Publish settings for the swf there is a "Permit debugging" box. If you tick that box your 1009 error will tell you which line has the error.

January 3, 2013

The permit debugging doesn't work.