12 Replies Latest reply on Jul 10, 2008 1:25 AM by AScracker

    getter and setter function  problem

    AScracker
      hi,
      i am trying to create the mxml component . here i am using the getter and setter function for visibleity .
      but it producing the error like this

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at components::Button_Component/set initialCount()[C:\Documents and Settings\senthilkumars\My Documents\Flex Builder 3\background\src\components\Button_Component.mxml:48]

      can any one help me hw to solve this issue

      ~~
      Sk
        • 1. Re: getter and setter function  problem
          peterent Level 2
          When you do this:
          <custom:Button_Component id="comp" height="87" initialCount="true,false,true" />

          The Flex Framework will set the properties of the component before it will create its children (technically, the setting of properties and the creation of children can be somewhat mixed, but the point is you cannot rely on the children being created before any properties are set).

          The initialCount property function is being called which then tries to change visible properties of the children; the children are not created yet and are thus null.

          Property setter and getter functions should not change the properties of UI children. Instead, have those functions call invalidateProperties() and override the commitProperties() function. Inside of commitProperties() you can then set those childrens' properties. This is a standard technique used throughtout the Flex source code.
          • 2. Re: getter and setter function  problem
            AScracker Level 1
            hi
            Could please explain how i need to add the code in my mxml . it'll be helpfull for me ...

            There are 10 mxml file are there . each file contain three button , so user need to call the component file when he need the three button are two button by setting the property initialCount="true,false,true" .
            ~~
            Sk
            • 3. Re: getter and setter function  problem
              ericbelair Level 1
              In the component, you can add a Event Listener and Handler for the creationComplete Event (which is triggered after all children have been created), and then in the Handler Function, set the visible property of each button to the value in the initialCount Array (see attached code). This is how I usually handle cases like this.
              • 4. Re: getter and setter function  problem
                AScracker Level 1
                hi eric
                Thanks for ur reply
                but i am facing some problem initialCount="false,true,true"
                var _mySubArray:Array = _initialCount[0].split(",");
                Alert.show(_mySubArray[0]+"--"+_mySubArray[1]+"--"+_mySubArray[2]);
                vans_but.visible = Boolean(_mySubArray[0]);
                try_but.visible = Boolean(_mySubArray[1]);
                sol_but.visible = Boolean(_mySubArray[2]);
                it is not working.if i pass the value like this means initialCount="0,1,1" it's working fine.
                where i did the mistake
                ~~
                Sk
                • 5. Re: getter and setter function  problem
                  ntsiii Level 3
                  This does NOT convert a string to a boolean:
                  Boolean(_mySubArray[0]);
                  It tests for non-null.

                  Instead do this:
                  vans_but.visible = (_mySubArray[0] == "true");

                  Tracy
                  • 6. Re: getter and setter function  problem
                    AScracker Level 1
                    hi,

                    Thanks it's working fine..
                    but after if i pressed any three button i need make visible for clickable one
                    hw i need to pass the array comp.initialCount="true,false,true"; is it the correct way

                    ~~
                    Sk
                    • 7. Re: getter and setter function  problem
                      peterent Level 2
                      Do you have to use a String or can you use an Array of 3 Boolean: initialCount="[true,false,true]" ? This would eliminate the need to turn the Strings into Booleans. Then you would have something like vans_but.visible=initialCount[0]; making your code a lot simpler.
                      • 8. Re: getter and setter function  problem
                        AScracker Level 1
                        Hi peterent,
                        thanks for ur answer it's working fine.but if i need to pass the value like this
                        when i pressed any button comp.initialCount="false,true,true"; it's not working fine. can you help me hw to solve this issue
                        ~~
                        Sk
                        • 9. Re: getter and setter function  problem
                          peterent Level 2
                          Why would you want to pass a string when an Array of Boolean would be so much easier:

                          initialCount="[true,false,true]"

                          ??
                          • 10. Re: getter and setter function  problem
                            AScracker Level 1
                            Hi peterent,
                            Actually i have used this ="[true,false,true]" in anchor tag
                            <custom:Button_Component id="comp" height="87" visibility="[true,false,true]" x="304" y="514" />.
                            it's working fine .
                            But if i pressed the answer button remaining two button are become invisible i.e( try_but ans sol_but). so hw should i call the function
                            comp.visibility ="[true,false,true]";

                            i got the error like this
                            1067: Implicit coercion of a value of type String to an unrelated type Array.

                            i do't know where i did the mistake . if you could help me then it'll be very much need full for me



                            ~~
                            Sk
                            • 11. Re: getter and setter function  problem
                              peterent Level 2
                              Just do:

                              comp._initialCount=[true,false,false];

                              without the quotes.
                              • 12. Re: getter and setter function  problem
                                AScracker Level 1
                                hi Peter Ent,
                                i did't get any success, i have used this line comp._initialCount=[true,true,false]; . but it's not working

                                ~~
                                Sk