R – Error while assigning the data to dataGrid Dataprovided

actionscript-3

I was trying to acces the datagrid at runtime and want to assign the data to it like

public function getGridData(reportsArray:ArrayCollection):void{

             reportData = reportsArray; 
             trace(" The length of the datagrid is "+reportData.length);
                        graphDataGrid.dataProvider = reportData;            
             invalidateDisplayList();                           

        }

but I was getting this error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at views.charts::ComparisonAnalysis/createDataGrid()[C:\FlexProjects\AdvancedMetering\views\charts\ComparisonAnalysis.mxml:32]
at views.charts::ComparisonAnalysis/getGridData()[C:\FlexProjects\AdvancedMetering\views\charts\ComparisonAnalysis.mxml:26]
at components.CustomReport::CustomReportSelector/getResultHandler()[C:\FlexProjects\AdvancedMetering\components\CustomReport\CustomReportSelector.mxml:147]
at components.CustomReport::CustomReportSelector/__reportingBase_dataGridResultChanged()[C:\FlexProjects\AdvancedMetering\components\CustomReport\CustomReportSelector.mxml:203]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298]
at components.CustomReport::ReportingBase/getDataFromServer()[C:\FlexProjects\AdvancedMetering\components\CustomReport\ReportingBase.as:98]
at components.CustomReport::ReportingBase/getResults()[C:\FlexProjects\AdvancedMetering\components\CustomReport\ReportingBase.as:78]
at components.CustomReport::CustomReportSelector/getGraphData()[C:\FlexProjects\AdvancedMetering\components\CustomReport\CustomReportScripts.as:20]
at components.CustomReport::CustomReportSelector/__getGraphandGridData_click()[C:\FlexProjects\AdvancedMetering\components\CustomReport\CustomReportSelector.mxml:248]

Can someboedy please let me know what the problem it looks like the datagrid is not instantiated at this ppoint of time

Sudee[

Best Answer

If the DataGrid is not instantiated at this time, due to deferred instantiation or general component lifecycle, I would create an ArrayCoolection property on the class like:

[Bindable] private var graphDataGridDataProvider:ArrayCollection;

and bind the graphDataGrid's dataProvider to this variable:

<mx:DataGrid id="graphDataGrid" dataProvider="{graphDataGridProvider}" />

and then in your getGridData method simply set the bound property.