Hi Guys,
I am having trouble getting the getItemAt method of the ArrayCollection class to work

in ActionScript even though it seems to work fine in MXML.
Any help greatly appreciated.

In the attached code lines 95->97 work fine whereas line 35 throws a "Cannot access a

property or method of a null object reference." error.

Best regards,
Anthony

<?xml version="1.0" encoding="utf-8"?>
	<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	creationComplete="sanData.send();dispalyData()" backgroundColor="#2888F8" 

themeColor="#009DFF" width="672" height="1170"
	 xmlns:ns1="assets.*" xmlns:local="*">


	

	<mx:Script>
	
	
		<![CDATA[
			import mx.rpc.events.ResultEvent;
			import mx.controls.dataGridClasses.DataGridColumn;
			import mx.collections.ArrayCollection;

		
			[Bindable]
			private var bookings:ArrayCollection;

	



			private function sanDataHandler(event:ResultEvent):void
			{
				bookings=event.result.data.row;
			}
			

			

			private function dispalyData():void
			{
				var temp = bookings.getItemAt(4).week;
				ShowNextWeek.text = " " + temp;
				
			}

			
			private function faultHandler

(event:mx.rpc.events.FaultEvent):void
			{
				var eventInfo:String="event target: 

"+event.target+"\n\n";
				eventInfo+="event type: "+event.type+"\n\n";
				mx.controls.Alert.show(eventInfo,"Event Information");	
			}
	
         		
			
		]]>
	</mx:Script>
	
	   
	<mx:HTTPService url="http://bogus.com/xml.php"
    id="sanData" 
    showBusyCursor="true" 
    result="sanDataHandler(event)"
    fault="faultHandler(event)"
    method="POST" />
    
 	
	<mx:Canvas x="10" y="22.4" width="629.2" height="1137.6" themeColor="#FFFFFF" 

borderStyle="solid" backgroundColor="#FFFFFF" borderColor="#EBE141" cornerRadius="4" 

borderThickness="7">

		<mx:DataGrid id="dataGrid" dataProvider="{bookings}" 

sortableColumns="true"  
		x="10" y="135"  height="377" alternatingItemColors="[#F0DFDF, #FFFFFF]" 
		borderStyle="outset" borderColor="#4DA5E3" fontSize="13" 

fontWeight="bold" width="595.2"> 
		<mx:columns>
		    <mx:DataGridColumn dataField="week"
		            headerText="Week"
		            headerStyleName="centered" 
		            width="50"
		            textAlign="center"/>
		            
		   	<mx:DataGridColumn dataField="day"
		            headerText="Day"
		            headerStyleName="centered" 
		            width="40"
		            textAlign="center"/>        
		    
		    <mx:DataGridColumn dataField="dept_name"
		            headerText="Class"
		            headerStyleName="centered" 
		            width="200"
		            textAlign="center"/>
		            
		     <mx:DataGridColumn dataField="room_name"
		            headerText="Room"
		            headerStyleName="centered" 
		            width="125"
		            textAlign="center"/>

		</mx:columns>
		</mx:DataGrid>

		<mx:Text id="ShowWeek" x="10" y="15"  width="576" 

text="{bookings.getItemAt(3).week}" />
	<mx:Text id="ShowNextWeek" x="10" y="35"  width="576" />  <!--	

text="{bookings.getItemAt(4).week}" /> -->
		<mx:Label x="10" y="55"   text="{sanData.lastResult.data.row[0].week}" 

/>
	</mx:Canvas>
	</mx:Application>

You are trying to access the bookings ArrayCollection when the creationComplete event is dispatched. At this time bookkings is null, so you get an Error.

The MXML inline calls work because they "hide" the error as long as bookings is null, and then work when the bookings ArrayCollection is setted.

You should wait for bookings to be populated, and call the disaplyData() method from the sanDataHandler() method instead of on the creationComplete event, to ensure that bookings is defined.

Thanks to http://forums.adobe.com/people/Sebastien%20V. for the answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.