PDA

View Full Version : Can't Access Array


richjamison
02-25-2008, 04:23 PM
I have a MovieClip "tableCell_mc" that is added to the stage via AS3. This MovieClip has a few variables that I have declared within it. These variables are as follows:

var tableCellHomeX:int;
var tableCellHomeY:int;
var myChildren:Array = new Array();

stop();

I use these to store unique data to each instance of tableCell_mc.

I can access the tableCellHomeX and tableCellHomeY vars no problem. It's the myChildren:Array that I'm having trouble with. It's an empty array but as I go through my for loop I try to add values to that array like so.

function makeTable(theXMLList:XMLList) {
var theXMLListAttribs:XMLList = theXMLList[0].attributes();
for (var i:int = 0; i < theXMLListAttribs.length(); i++) {
tableHeader = new tableCell_mc();
tableHeader.width = stage.stageWidth / theXMLListAttribs.length();
tableHeader.x = 0;
tableHeader.y = 100;
tableHeader.tableCellHomeX = tableHeader.width * i;
tableHeader.tableCellHomeY = 100;
tableHeader.cellValue_text.text = theXMLListAttribs[i].name();
tableHeader.addEventListener(Event.ADDED_TO_STAGE, slideToRight);
tableHeader.addEventListener(TweenEvent.MOTION_FIN ISH, dummyFunction);
addChild(tableHeader);
for (var j:int = 0; j < theXMLList.length(); j++) {
var myAttribs:XMLList = theXMLList[j].attributes();
tableHeader.myChildren[j] = String(myAttribs[i]);
}
}
}



I get the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at SpreadSheet_fla::MainTimeline/makeTable()
at SpreadSheet_fla::MainTimeline/frame2()


Help anyone?

Thanks,

Rich

richjamison
02-25-2008, 06:46 PM
Ok, so I tried accessing an array in a bare bones fla file that I named Work1.fla.

I created a MovieClip that I named "myMovieClip_mc" and drug an instance onto the stage. Gave it an instance name of "myInstance_mc". Inside that MovieClip I have declared an array like so:

var myArray:Array = new Array("a", "b", "c");
trace(myArray); //trace: a,b,c
stop();


On the main timeline I have the following code:

trace(myInstance_mc.myArray); //trace: null

still no dice! I am baffled.

Flashonacci
02-25-2008, 07:10 PM
That is happening because the code to access the array is being executed before the movieclip containing the array has been initialized. If you used a
setTimeout(function,delay); it would work.

function accessArray(){
trace(myInstance_mc.myArray);
}
setTimeout(accessArray,10);

richjamison
02-25-2008, 08:48 PM
Totally makes sense now. Noob mistake:mad:

Thanks again.