Hello there and happy New Year,

I'm having problems with dynamic instances and databinding.
Here a little example:

ExtLabel.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml"; text="{this.name}">
</mx:Label>
That is just a simple Label-Component printing out its name.
If I now initialize that label in my main.mxml everything works fine:

main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" applicationComplete="init()">
<mx:Script>
     <![CDATA[
         import Test.ExtLabel;

         public function init():void {
             var label:Object = new ExtLabel();
             addChild(DisplayObject(label));
         }
     ]]>
</mx:Script>
</mx:Application>
But if I use getDefinitionByName to dynamically create an instance of
ExtLabel I get a Null-reference exception:

main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" applicationComplete="init()">
<mx:Script>
     <![CDATA[
         public function init():void {
             var labelClass:Class =
flash.utils.getDefinitionByName("Test.ExtLabel") as Class;
             var label:Object = new labelClass();
             addChild(DisplayObject(label));
         }
     ]]>
</mx:Script>
</mx:Application>
I'm using the compiler directive "--includes" to include the class so
that's not the problem.
The problem seems to be the databinding text="{this.name}". If I get rid
of it, everything works fine.
The error I receive (sorry it's in german):
TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine
Methode eines null-Objektverweises ist nicht möglich.
     at Test::ExtLabel/initialize()
     at
mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::childAd\
ded()
     at
mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::childAdde\
d()
     at mx.core::Container/addChildAt()
     at mx.core::Container/addChild()
     at main/init()
     at main/___Application1_applicationComplete()
     at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
     at flash.events::EventDispatcher/dispatchEvent()
     at mx.managers::SystemManager/::preloader_preloaderDoneHandler()
     at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
     at flash.events::EventDispatcher/dispatchEvent()
     at mx.preloaders::Preloader/::displayClassCompleteHandler()
     at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
     at flash.events::EventDispatcher/dispatchEvent()
     at mx.preloaders::DownloadProgressBar/::timerHandler()
     at mx.preloaders::DownloadProgressBar/::initCompleteHandler()
     at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
     at flash.events::EventDispatcher/dispatchEvent()
     at mx.preloaders::Preloader/::dispatchAppEndEvent()
     at mx.preloaders::Preloader/::appCreationCompleteHandler()
     at
flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEven\
tFunction()
     at flash.events::EventDispatcher/dispatchEvent()
Thanks for your help!
Bastian

Reply via email to