I’m using an AS3 class that is:
package {
public class PeopleInfo {
public var elements:int;
public var PeopleName:Array;
public var PeopleInt:Array;
public var PeopleDecimal:Array;
}
}
In another file I’ve got:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
creationComplete="initApp()"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
public var ii:int;
public var dataWriteToDB:PeopleInfo = new PeopleInfo;
public function initApp():void
{
// Initialize data provider array.
var int_data:Array = new Array(10, 20, 30, 40, 50);
var decimal_data:Array = new Array(13.11, 23.34, 35.69, 43.29, 58.92);
var name:Array = new Array("Joe", "Karen", "Simon", "Greg", "Alice");
dataWriteToDB.elements = 5; // number of elements in array
for (ii = 0; ii < dataWriteToDB.elements; ii++)
{
dataWriteToDB.PeopleName[ii] = name[ii];
dataWriteToDB.PeopleInt[ii] = int_data[ii];
dataWriteToDB.PeopleDecimal[ii] = decimal_data[ii];
}
}
and so on...
I’m getting a run-time error: Error #1009: Cannot access a property of method of a null object reference referring to the for loop’s first line dataWriteToDB.PeopleName since it is NULL.
I’m guessing the problem here is that while dataWriteToDB is declared initially, the array lengths for the arrays in PeopleInfo class have not been set yet. Or, not sure otherwise why it’s NULL. Anyone know how to clear this up?
The arrays have not been initialized in the class. You should do it in the declaration:
Also consider using
pushto add elements to the array, to avoid accidentally accessing a non-existing index: