How to avoid error #1009 while populating a chart on User Click event in a widget

388
2
05-21-2012 08:29 PM
vijipat
New Contributor
Hi,

I am trying to populate a chart when user clicks on a road feature(a buffer is drawn around a road feature and all the population information is fetched from other layer).
But when init() method gets called, I get ???TypeError: Error #1009: Cannot access a property or method of a null object reference.???  for following code:
<mx:ColumnChart id="columnChart"
    dataProvider="{queryTask.executeLastResult.attributes}"
    visible="{queryTask.executeLastResult != null}">
</mx:ColumnChart>
As query is not yet fired, I get this error. But for Datagrid display it does not give same error. What shall be done to resolve this.

Please help.

Thanks & Regards,
Viji
Tags (2)
0 Kudos
2 Replies
BenKane
New Contributor III
You could set the dataProvider and visible properties in the query function.  For example:

function queryOnClick():void
{
  /** execute your query, then add the following: */
  columnChart.dataProvider = queryTask.executeLastResult.attributes;
  if(queryTast.executeLastResult != null)
   {
      columnChart.visible = true;
   }else{
      columnChart.visible = false;
   }
}
0 Kudos
IvanBespalov
Occasional Contributor III
TypeError: Error #1009: Cannot access a property or method of a null object reference comes from this part of code

queryTask.executeLastResult.attributes

it does not matter where you use this code
in ColumnChart tag
<mx:ColumnChart id="columnChart"
dataProvider="{queryTask.executeLastResult.attributes}"
visible="{queryTask.executeLastResult != null}">
</mx:ColumnChart>

or in script tag
function queryOnClick():void
{
  /** execute your query, then add the following: */
  columnChart.dataProvider = queryTask.executeLastResult.attributes;
  if(queryTast.executeLastResult != null)
   {
      columnChart.visible = true;
   }else{
      columnChart.visible = false;
   }
}


If the queryTask is never executed, then queryTask.executeLastResult is null!!!
So queryTask.executeLastResult.attributes returns TypeError: Error #1009: Cannot access a property or method of a null object reference

example / rule
var myAnyTypeObject:* = null;
var somePr:* = myAnyTypeObject.someproperty; // returns TypeError: Error #1009: Cannot access a property ...
myAnyTypeObject.executeSomeFunction();// returns TypeError: Error #1009: Cannot access ... method ...


Right?

I would suggest to take Ben's code with some changes
function queryOnClick():void
{
  if(queryTask.executeLastResult != null)
   {
      columnChart.dataProvider = queryTask.executeLastResult.attributes;
      columnChart.visible = true;
   }else{
      columnChart.visible = false;
   }
}
0 Kudos