5 Replies Latest reply on May 29, 2009 5:54 PM by Andre Brito

    Error when adding objects dynamically to a TileList

    Andre Brito Level 1

      Hi people.

      I'm having some troubles when adding things dynamically. I have a Thing with a Label and I want to add the Thing in a TileList with a name (a String) that I have. The TileList's dataprovider is an ArrayCollection called array.

       

      In ActionScript, something like this:

      private function addThing(event : MouseEvent) : void

      {

      var thing : Thing = new Thing();

       

      var name : String = new String("test");

       

      thing.label.text = name;

       

      array.addItem(thing);

      }

       

      But there's a runtime-error (an exception, I believe):

      TypeError: Error #1009: Cannot access a property or method of a null object reference.

       

      Why this is happening? When I try to add the Thing to the Box that I have (forgot to say that the TileList is inside a Panel and the Panel inside a VBox) things go well (actually, I have to write the attribution bellow the addChild).

       

      Thanks! And sorry my bad english.

        • 1. Re: Error when adding objects dynamically to a TileList
          Gregory Lafrance Level 6

          Has the array been initialized?

           

          If you just did this:

           

          private var array:ArrayCollection;

           

          that's not enough. You need to do this:

           

          private var array:ArrayCollection = new ArrayCollection();

           

          If this post answers your question or helps, please mark it as such.

          • 2. Re: Error when adding objects dynamically to a TileList
            Andre Brito Level 1

            Hi Greg.

            Yes, the array is initialized.

            I forgot to say, so I'm saying it now. The Thing.mxml file has only mxml code. I'm saying this 'cause I believe that that error happens because there's no constructor for Thing. But for Thing to have a constructor I'd had to code Thing.as, wouldn't I?

             

            The error happens when I try to name "thing":

             

            thing.label.text = name;

             

            Edit: I noticed somehting: the initialization of Thing happens after the "addItem" is called. I created a method for the creationComplete called init() and that init() method is called after the addItem is called. Is that right? Why that happens?

            • 3. Re: Error when adding objects dynamically to a TileList
              Andre Brito Level 1

              Guys, nobody knows how to solve this? I'm accepting new sugestions.

               

              My "main doubt" is this: when I create a component using the "new" word, why it is creating twice, and why does the creationComplete method is called after I add that component to the array?

              • 4. Re: Error when adding objects dynamically to a TileList
                *Prashant Shelke* Level 4

                If you will provide proper text here it will helpful to trac issue. Can you please provide your mxml here(if its just case study!) I suspect label to which you are assigning text is not initialized.


                Thanks.


                • 5. Re: Error when adding objects dynamically to a TileList
                  Andre Brito Level 1

                  Hi Prashant Shelke,

                  Sorry about that.

                   

                  I'll explain my problem again and there I'll post my MXML code. It's just a case study

                   

                  There's a Panel (thePanel), with a TileList (theList) and its ControlBar with a Button: "Create new Thumb". When I press that Button, a TitleWindow (theEnterTheNameTitleWindow) appears, with a TextInput and a Button (theOkButton). After I type something in that TextInput, I click theOkButton and I want a new Thumb (theNewThumb) with the name I typed to get in the the TileList (through the ArrayCollection - theNewThumb is added to a array, and this array is the dataProvider of the TileList) (that means that theLabelOfTheThumb will be the String that I typed there).

                   

                  I have, in the object that represents that theEnterTheNameTitleWindows that I said, a reference to the parent of this TitleWindow. I don't know if this is a good pratice (please, let me know), but I have to add theThumb in the ArrayCollection that is on the parent - thePanel (because that ArrayCollection is the dataProvider for the tileList, as I said before). So the method "processThings" above is what is called before "theOkButtonWasClicked".

                   

                  Here's the piece of code that handles the "theOkButton was clicked Event":

                  private var parent : Panel;


                  public function processThings(theParent : Panel) : void

                  {

                     parent = theParent;

                  }


                  private function theOkButtonWasClicked() : void

                  {

                     if (textTypedIsCorrect() == true)

                     {

                        PopUpManager.removePopUp(this);

                   

                        var name : String = textInputInTheWindow.text;

                   

                        var theThumb : Thumb = new Thumb();

                   

                        theThumb.TheName = name;

                   

                        parent.addInTheArrayCollectionThatIsTheDataProvider(theThumb);

                     }

                  }

                   

                  Ok.

                  The Thumb.mxml looks like this (the names have been changed, because it was all in portuguese):

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="100"

                  horizontalGap="10" verticalAlign="middle" paddingLeft="10">

                   

                     <mx:Script>

                        <![CDATA[

                            [Bindable]

                            private var theName : String = new String("Without a name");

                   

                            public function get TheName() : String { return theName; }

                   

                            public function set TheName(theNewName : String) : void { theName = theNewName;

                         ]]>

                     </mx:Script>

                   

                   

                    <mx:HBox>     

                         <mx:Image id="theImage" source="@Embed('images/file.png')" width="70" height="54"/>

                     </mx:HBox>

                   

                     <mx:Label id="theLabelOfTheThumb " fontSize="11" text="{this.TheName}"/>

                   

                  </mx:HBox>

                   

                  Ok, now I guess that there's some code that you guys can check it out

                  Thanks guys. BTW, I really would like some suggestions if you guys can't fix that... This problem is driving me crazy (I can't stop thinking about it - even when I'm not programming).

                   

                  Bye!

                   

                  Edited.

                  Hi guys. I guess I solved the problem (not in the way that I wanted, but it did). I was searching in TourDeFlex for this, and I saw that there's no itemRenderer in the use of the TileList. So I created an object, instead of that Thumb. And kinda worked very well

                   

                  I don't know if this is simpler or faster or better (actually, guess it is faster, but there's the loss of the non-reusable Thumb), but it solved. If someone could say for me (if that was a good one) I would aprecciate it

                   

                  If someone wanna try to solve using the Thumb, go for it. I was reading a few things about components creation, and I even implemented some overrided methods (extended from UIComponent) for creation, but it kinda did nothing (only stopped the erro from happening). I guess that there's a way that someone can solve using these things that I said.

                   

                  Bye!

                   

                  At last, but not least, I'm really sorry my english.