# XML and AS 3.0

**URL:** <https://forum.kirupa.com/t/xml-and-as-3-0/286500>\
**Category:** flash\
**Created:** [April 16, 2009, 11:29am UTC](https://forum.kirupa.com/t/xml-and-as-3-0/286500 "2009-04-16T11:29:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![deerowbear](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@deerowbear](https://forum.kirupa.com/u/deerowbear)\
**Post date:** [April 16, 2009, 11:29am UTC](https://forum.kirupa.com/t/xml-and-as-3-0/286500/1 "2009-04-16T11:29:45Z")

</div>

Hi there, I am writing a class in AS 3.0 to read in a xml file to create content for a quiz.  
Once I instantiate the xml obj and pass in a string that determines the xml name in the constructor for the xml class I read in the xml and populate some arrays. Evidently in AS 3.0 you have listeners for reading in xml, which is the problem I am having. Once the object is instantiated I try to call on a method called getArray, but the XML hasn’t finished being read in and I get a compile error. Error below:  
TypeError: Error #1009: Cannot access a property or method of a null object reference.  
at xmlReader\_fla::MainTimeline/frame1()  
Any help is appreciated. Daniel  
//Here is the call to the class

```auto
import com.xmlReader.xmlReader;
var myXML:xmlReader = new xmlReader("navdata.xml");
var myArray:Array = myXML.getArray();
for (var v:Number = 0; v < myArray.length; v++)
{
 addChild (myArray[v]);
}

```

Here is the class:

```auto
 
package com.xmlReader
{
 import fl.controls.RadioButton;
 import flash.events.Event;
 import flash.net.URLLoader;
 import flash.display.*;
 import flash.net.URLRequest;
 import flash.events.IOErrorEvent;
 public class xmlReader extends MovieClip
 {
  var labelArray:Array = null;
  var answerArray:Array = null;
  public var btnArray:Array = null;
  public var loader:URLLoader = new URLLoader();
  public function xmlReader (myFile:String):void
  {
   loader.addEventListener (Event.COMPLETE, onComplete, false, 1, false);
   loader.addEventListener (IOErrorEvent.IO_ERROR, onIOError, false, 1, false);
   loader.load (new URLRequest(myFile));
  }
  public function onComplete (evt:Event):void
  {
   labelArray = new Array ;
   answerArray = new Array ;
   var question:String = "";
   try
   {
    var navData:XML = new XML(evt.target.data);
    for each (var textQ:XML in navData.question.testText)
    {
     trace (textQ.questionText);
     trace (textQ.questionXML);
     for each (var labelGUI:XML in textQ.parent().check)
     {
      if (labelGUI.attribute("labelName") != null || labelGUI.attribute("answer") != null)
      {
       labelArray.push (labelGUI.attribute("labelName").toString());
       answerArray.push (labelGUI.attribute("answer").toString());
      }
     }
    }
    createGUI (labelArray, answerArray);
    loader.removeEventListener (Event.COMPLETE, onComplete);
    loader.removeEventListener (IOErrorEvent.IO_ERROR, onIOError);
   }
   catch (err:Error)
   {
    trace ("Could not parse loaded content as XML:
" + err.message);
   }
  }
  public function onIOError (evt:IOErrorEvent):void
  {
   trace ("An error occurred when attempting to load the XML.
" + evt.text);
  }
  public function createGUI (labelArray:Array, answerArray:Array):void
  {
   btnArray = new Array(labelArray.length);
   for (var i:Number = 0; i < btnArray.length; i++)
   {
    btnArray* = new RadioButton();
    btnArray*.label = labelArray*;
    btnArray*.move (20,20+i * 50);
   }
  }
  public function getArray ():Array
  {
   /*if (btnArray == null)
   {
    getArray();
   }*/
   return btnArray;
  }
 }
}

```
