views:

23

answers:

1

I have a weird issue (weird because it is specific to one component) with applicationComplete in a fairly simple application. All the UI components are declared in MXML. I can access them all in applicationComplete, but not a spark.components.TextArea component, named taStatus here; it is null in the handler.

MXML looks sort of like this (there are lots of other components, but nothing special)

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="710" minHeight="640" applicationComplete="onApplicationComplete(event)" width="710" height="640">
    <mx:TabNavigator left="15" right="15" top="15" bottom="340" paddingTop="0">
        <s:NavigatorContent label="General" width="100%" height="100%">
            <s:Label x="93" y="71" text="Label" id="lblTest"/>
        </s:NavigatorContent>
        <s:NavigatorContent label="Status" width="100%" height="100%">
            <s:TextArea id="taStatus" width="100%" height="100%" text="Startup." editable="false"/>
        </s:NavigatorContent>
    </mx:TabNavigator>
    <fx:Script source="main.as" />
</s:Application>

Here is the handler in main.as

protected function onApplicationComplete(event: FlexEvent) : void
{
    lblTest.text = 'abc789';    // OK
    taStatus.text = 'abc789';   // Fail 
}

TypeError: Error #1009: Cannot access a property or method of a null object reference. So taStatus is null... What is so special about this TextArea?

Update 2010-06-12 02:53 Moving the NavigatorContent (tab) above all other tabs suddenly makes the TextAreas get instantiated on time. Very strange, because all the components are definitely being created; I can see them.

+2  A: 

It's because the TextArea is in a child of the TabNavigator that is not the first child, so by default it is not instantiated until the user opens that tab.

Your options are to either wait until the user opens that tab to do whatver you need to do to set up the TextArea or change the child creation policy on the TabNavigator to create all its children at startup rather than waiting for them to be clicked.

To do that, you need to set the creationPolicy property on the TabNavigator to "all".

DanK
A perfect solution/answer. Thanks DanK; this was starting to drive me nuts ;-)
Alan G.