0
votes

Can someone explain what im doing wrong? I'm simply trying to set the icon for a button in a Flex Mobile project using actionscript. I keep getting a null error reference. But i'm 100% sure that is not a bad path. I've used the same path a dozen other times in the app.

var editCalloutBtn:CalloutButton = new CalloutButton;
if(value.type=="reminder")
{
            //this is where the null error happens
        editCalloutBtn.iconDisplay.source="assets/images/reminderIcon_45.png";

}else
{
        editCalloutBtn.iconDisplay.source="assets/images/deadlineIcon_45.png";
}
callout_group.addElement(editCalloutBtn);

For a test, I set the icon using the same path doing everything in mxml, and it works. So it's not the path. But I need to do this with actionscript so I can set the value dynamically.

Any ideas?

1
are you sure that iconDisplay is the correct attribute? can't find it in here: help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/… - Philipp Kyeck
It comes up in the code hinting. I can't think of what else it would be. Kinda silly if you can only set the icon once with mxml. - brybam

1 Answers

2
votes

Flex components are created in a lazy fashion. That means that any subcomponents will not be created until they are actually needed. And as long as a component has not been added to the displaylist you can be sure its subcomponents will not be needed (you can Google "Flex component life cycle" if you want to get to know more about it).

More concrete for your case

You try to access 'iconDisplay' before the CalloutButton was added to the displaylist, hence the 'iconDisplay' property is 'null'.

Solution

I'm not exactly sure why you want to do this in ActionScript, but here's the fix anyway: you have to listen for FlexEvent.CREATION_COMPLETE. When that event is fired by the CalloutButton, you will be certain that its 'iconDisplay' subcomponent is no longer 'null'.

var editCalloutBtn:CalloutButton = new CalloutButton();
editCalloutBtn.addEventListener(
    FlexEvent.CREATION_COMPLETE, onCalloutButtonReady);
callout_group.addElement(editCalloutBtn);

private function onCalloutButtonReady(event:FlexEvent):void {
    //remove the event listener: we no longer need it
    editCalloutBtn.removeEventListener(    
        FlexEvent.CREATION_COMPLETE, onCalloutButtonReady);

    //no longer null
    trace(editCalloutBtn.iconDisplay); 
}

There can be exceptions to this (when using states for example, or a skin that doesn't have an 'iconDisplay' element), but I'm not going to bother you with that, because they shouldn't happen here.