I have created a custom component in an MXML file based on a
VBox that contains another custom component.
Something like below (I will call it CustomVBox for
<!-- file name CustomVBox.mxml -->
Now - in an action script file I wish to create the
var comp:CustomVBox = new CustomVBox().
The problem is that the CustomRow inside the CustomVBox
component is now a public member that my action script can refer
to, but calling the constructor will not instantiate the sub
component. So if I wanted to set some property on the inner
CustomRow object, I get a null reference exception.
If within my action script I addChild(comp) first, that
creates the child components and I am okay. However, I want to be
able to set all my properties up front. So I could just save off
any settings and use them when createChildren() is called. But the
CustomRow, being an mxml tag in my CustomVBox mxml file, is still
public and nothing prevents a user from accessing it directly and
getting the null reference before createChildren() got called.
How do I prevent this? Am I forced to write it all in
actionscript so that my subcomponent can be made private or can I
still write it in mxml and prevent the null reference exception?
You need to listen to flex event creationComplete, or
override childrenCreated protected UIComponent method to inform you
on that occasion, or change creationPolicy to
ContainerCreationPolicy.ALL (this will ensure that children are
built once you instantiate the component). However, the last way
isn't preferable because this will force your component into
creating children that may be invisible at the creation