21 Replies Latest reply on Dec 11, 2008 8:17 AM by Newsgroup_User

    Variable not seeing data on another page

    spacehog Level 1
      I am trying to read the length of an array on my Application page from a component. In the component I have this code:

      <mx:ApplicationControlBar xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%" creationComplete="getDPprops()">
      <mx:Script>
      [Bindable]
      public var dpLen:int;
      private function getDPprops():void
      {
      dpLen = Application.application.flatDP.length;
      }
      </mx:Script>

      On the Application page I have a handler tied to a method that creates the FlatDP:

      public function returnHandler(e:ResultEvent):void
      {
      flatDP = ArrayCollection(e.result);
      }

      The odd thing is that when I run the app I get an error message: "TypeError: Error #1009: Cannot access a property or method of a null object reference." referring back to the line where I define dpLen. Which makes me think that flatDP is NULL. But if I place a label on the same page where the error occurs with a bindable link back to the length I get the correct length of flatDP: 625

      <mx:Label text="{Application.application.flatDP.length}"/>

      Why does the reference Application.application.flatDP.length work in the label text, but not when I try to attach it to a variable in a function?
        • 1. Re: Variable not seeing data on another page
          ntsiii Level 3
          Probably the child component creationComplete is running the assignment before the rpc call in the main app has completed.

          One way to avoid this is to use binding to pass flatDP into the component when it changes.

          Another way is to use a ChangeWatcher in the component that fires when flatDP changes.

          Tracy
          • 2. Re: Variable not seeing data on another page
            ntsiii Level 3
            Oh, the label works because it IS a binding.
            Tracy
            • 3. Re: Variable not seeing data on another page
              rtalton Level 4
              I'd go the other way:
              In the main app, set the component's public var "dpLen" in returnHandler, and also set it as Bindable in the main app when you do the assignment. Now you know you have the value, and it's bound.
              After that, you can use it anywhere in the component.

              You set it to Bindable in the component, but dpLen won't get updated when the ArrayCollection changes because you haven't bound it to flatDP.length.
              • 4. Re: Variable not seeing data on another page
                spacehog Level 1
                What's funny is that in the main app flatDP is set to Bindable. So I thought all of its properties (length, source, etc.) would also be bindable.
                • 5. Re: Variable not seeing data on another page
                  rtalton Level 4
                  Do you have a working sample we can see?
                  • 6. Re: Variable not seeing data on another page
                    spacehog Level 1
                    If I had a working sample, I wouldn't be asking this question ;)

                    Here is the code for the main app:

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:view="com.views.*" layout="vertical" creationComplete="initApp(event)">
                    <!--<mx:Script source="asincludes/Dashboard.as" />-->
                    <mx:Script>
                    <![CDATA[
                    import mx.charts.HitData;
                    import mx.collections.ArrayCollection;
                    import mx.controls.Alert;
                    import mx.rpc.events.ResultEvent;

                    [Bindable]
                    public var flatDP:ArrayCollection;
                    [Bindable]
                    public var nNumMonths:int;
                    [Bindable]
                    public var nBegin:int;
                    [Bindable]
                    public var nEnd:int;
                    [Bindable]
                    public var pgeTitle:String;
                    [Bindable]
                    public var alertStat:int;
                    [Bindable]
                    public var toolGroup:int;

                    private function initApp(e:Event):void
                    {
                    chartmth_RO.qMonthlyUsage();
                    }

                    public function returnHandler(e:ResultEvent):void
                    {
                    flatDP = ArrayCollection(e.result);
                    dg.dataProvider = flatDP;
                    contLine.lineMMR.dataProvider = flatDP;
                    }
                    ]]>
                    </mx:Script>

                    <mx:RemoteObject id="chartmth_RO" showBusyCursor="true" destination="ColdFusion" source="caeusage.src.data.charts_usage_mth">
                    <mx:method name="qMonthlyUsage" result="returnHandler(event)" />
                    </mx:RemoteObject>

                    <mx:Label text="{flatDP.length}"/>
                    <mx:DataGrid id="dg" width="75%"/>
                    <view:cntrLC id="contLine"/>

                    </mx:Application>
                    ------------------------------------------------------------------------------------------ --------------------------------------------------
                    <!-- Here is the composite component (cntrLC) I call at the end of the main app -->
                    <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%" xmlns:view="com.views.*">

                    <view:cBarMMR id="acbMMR"/>
                    <view:chartMMR id="lineMMR"/>
                    </mx:VBox>
                    ------------------------------------------------------------------------------------------ --------------------------------------------------------------
                    <!-- This is the component cBarMMR that is having the issue I have added comments with *s in them to show where the errors occur-->

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:ApplicationControlBar xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%" creationComplete="getDPprops()">
                    <mx:Script>
                    <![CDATA[
                    import mx.collections.ArrayCollection;
                    import mx.core.Application;

                    [Bindable]
                    public var nBegin:int;
                    [Bindable]
                    public var nEnd:int;
                    [Bindable]
                    public var dpLen:int;
                    private function getDPprops():void
                    {
                    //dpLen = Application.application.flatDP.length;<!-- ****The error occurs here****-->
                    set_Thumbs(0);
                    }
                    private function formatSliderDataTip(val:Number):String
                    {
                    return String(Application.application.flatDP[val-1].abbrvDT);
                    }
                    [Bindable]
                    public var lineData_array:Array = new Array();
                    [Bindable]
                    public var arrayDP:Array = new Array();
                    /* private function filterFlatDP(p_evt:Event):void <!-- ****Also if I uncomment this function I get the same error message****-->
                    {

                    var arrayDP:Array = Application.application.flatDP.source;
                    var thumb1:int = p_evt.target.values[0];
                    var thumb2:int = p_evt.target.values[1];

                    if(thumb1 < thumb2-1)
                    {
                    lineData_array = arrayDP.slice(thumb1, thumb2);
                    }
                    } */
                    public function move_Thumbs(m:int):void{
                    var move:int;
                    move = nBegin + m;
                    set_Thumbs(move);
                    }
                    public function set_Thumbs(t1:int):void{
                    var t2:int;
                    nBegin = t1;
                    nEnd = t1 + 120;
                    }
                    ]]>
                    </mx:Script>
                    <mx:Label text="{Application.application.flatDP.length}"/>

                    <mx:HSlider id="dateFilter_sldr"
                    width="100%"
                    thumbCount="2"
                    allowThumbOverlap="false"
                    minimum="1" maximum="{dpLen}"
                    dataTipFormatFunction="formatSliderDataTip" showDataTip="true"
                    values="{[nBegin, nEnd]}"
                    tickInterval="24" snapInterval="24"
                    allowTrackClick="true" liveDragging="true"
                    change="filterFlatDP(event)"
                    />

                    <!---->
                    </mx:ApplicationControlBar>
                    • 7. Re: Variable not seeing data on another page
                      ntsiii Level 3
                      Instead of using the function getDPprops to fetch the data, pass it into the component.

                      First, in the component, set up a property to receive it:
                      [Bindable]
                      public var flatDP:ArrayCollection;

                      Note: if you want to assign the local property value vars, like "nBegin", use a setter function instead of a public var, and assign those values in that function.

                      Then pass a reference to flatDP into the component using binding:

                      <view:cntrLC id="contLine" flatDP="{this.flatDP}" />

                      Whenever flatDP is updated in the main app, the value in the component will be updated, and allits bindings will fire.

                      Tracy
                      • 8. Re: Variable not seeing data on another page
                        ntsiii Level 3
                        Note: There are many other ways to do this kind of thing.

                        I typically use a singleton data model and ChangeWatchers.

                        Tracy
                        • 9. Re: Variable not seeing data on another page
                          rtalton Level 4
                          I did some thinking on this and it ocurred to me that we want to keep this component independent of anything going on in the main app. So I thought I'd make an example showing a component which binds to an array's length property, all done in the main app. All the component needs is a bindable public variable, that's it. The main app binds the array length property to the var within the mx: tag. Any changes to the array's length are then broadcast to the component.
                          http://www.anaheimwib.com/_comps/binding/
                          Check out the sample app; it has source view enabled. You can see a label on the control bar being updated with the array's length whenever you change the array by adding/removing items.

                          • 10. Re: Variable not seeing data on another page
                            spacehog Level 1
                            rtalton -

                            You will encounter the same problem in your example as I am having. I can see bound data from the main app as long as I reference it in the MXML tag with {}. Just like you did :

                            <mx:Label id="lblLength" text="{_acLength}"/>

                            However, if I need to use that value in a script on that page that is when the error occurs:

                            [Bindable]
                            public var dpLen:int;

                            public function someFunc():void
                            {
                            dpLen = _acLength;

                            }

                            The function will see _acLength as 0 and not the true length
                            • 11. Re: Variable not seeing data on another page
                              rtalton Level 4
                              I just tried it and I can see a value in the variable _acLength when I examine it in a function called on the component's creation complete event.
                              I made a quick change to the component to test...can you copy and paste this code and try it yourself?
                              The main app code has not changed from my previous post.
                              Let me know what happens.
                              • 12. Re: Variable not seeing data on another page
                                spacehog Level 1
                                trying it now.
                                • 13. Re: Variable not seeing data on another page
                                  spacehog Level 1
                                  Odd. I was able to get your example to work. I even modified it like this:

                                  <?xml version="1.0" encoding="utf-8"?>
                                  <mx:ApplicationControlBar xmlns:mx=" http://www.adobe.com/2006/mxml"
                                  creationComplete="initApp()" >
                                  <mx:Script>
                                  <![CDATA[
                                  [Bindable]
                                  public var _acLength:int;
                                  [Bindable]
                                  public var newVar:int;
                                  private function initApp():void{
                                  trace(_acLength);//value =4
                                  newVar = _acLength;
                                  }
                                  ]]>
                                  </mx:Script>
                                  <mx:Label text="Number of items in Data Grid:"/>
                                  <mx:Label id="lblLength" text="{_acLength}"/>
                                  <mx:Label text="NewVar value:"/>
                                  <mx:Label id="lblnewVar" text="{newVar}"/>
                                  </mx:ApplicationControlBar>

                                  But in my script it is still not working. I am going to have to look at this harder because I use the same steps, just different names

                                  Thanks

                                  • 14. Re: Variable not seeing data on another page
                                    rtalton Level 4
                                    Progress!
                                    This is probably some kind of timing issue. I think you have the main app calling a component which calls two more components, so you may have to look into whether that first comp in the main is actually finished being created before accessing its "children's" props. I'm not too knowledgable on that topic, so maybe someone else can jump in here and enlighten both of us.
                                    • 15. Re: Variable not seeing data on another page
                                      spacehog Level 1
                                      I agree on the timing issue. I am looking into the singleton model that Tracy mentioned. I was already trying to implement it because it solves the issue of what each view can see. I have a skeleton built with a single DataHolder class that is only accessible via a single static function. I am new to this type of methodology and for me the learning curve is slow, so I was hoping for a quick fix in order to meet a deadline next week.

                                      It looks like I will just have to pull some all-nighters to do this.

                                      Thanks
                                      • 16. Re: Variable not seeing data on another page
                                        Level 7

                                        "rtalton" <webforumsuser@macromedia.com> wrote in message
                                        news:ghp0s4$cne$1@forums.macromedia.com...
                                        > Progress!
                                        > This is probably some kind of timing issue. I think you have the main app
                                        > calling a component which calls two more components, so you may have to
                                        > look
                                        > into whether that first comp in the main is actually finished being
                                        > created
                                        > before accessing its "children's" props. I'm not too knowledgable on that
                                        > topic, so maybe someone else can jump in here and enlighten both of us.

                                        I didn't jump in with this because it was originally posted a couple of
                                        hours before I posted my Flex FAQ, which I felt was answer enough it the OP
                                        was observant enough to see it and go read it (Q3). I personally feel that
                                        the road he's trying to go down is a dead end for a lot of reasons. I also
                                        feel that you shouldn't jump into using the Singleton pattern to solve these
                                        types of issues, but instead Singleton data should only be used when it's
                                        making sure that there's only one copy of certain data that is referenced
                                        everywhere, and it still be pushed down through all levels using the
                                        composite pattern so that they can remain loosely coupled--they don't need
                                        to know that the data they're getting is a Singleton. Some people consider
                                        Singleton to be an anti-pattern , so be very, very sure you understand what
                                        it does and what problems it is intended to solve rather than using it as a
                                        workaround to avoid learning the proper way to do things.

                                        You may want to read this:
                                        http://blog.iconara.net/2008/04/13/architectural-atrocities-part-x-cairngorms-model-locato r-pattern/

                                        Pay special attention to the paragraphs starting with this one:
                                        Even in the Cairngorm examples there seems to be some confusion as to what
                                        good the Model Locator pattern does. In the Cairngorm Store example
                                        application some components bind directly to the Model Locator instance but
                                        some don’t and instead get their data from their parent. I haven’t seen any
                                        rationalization for this, so I’m going to propose one myself: binding to a
                                        global variable makes components less reusable. For some reason, in
                                        Cairngorm this wisdom applies only to some components and this makes me
                                        think that the designers of the framework (or at least the ones writing the
                                        example application) at least subconsciously have understood how bad global
                                        variables are.

                                        And then, near the end, check out the section that is labeled "Dependency
                                        Injection"

                                        HTH;

                                        Amy


                                        • 17. Re: Variable not seeing data on another page
                                          rtalton Level 4
                                          Hi Amy,
                                          Looked at the article-will go back tonight to delve into it in more detail, thanks for the link.
                                          I'll be the first to admit that design patterns, model locators, singletons, etc., are more than a little confusing. So, just speaking for myself, I like to see concrete examples on how to solve a problem, along with an explanation on why this jibes with a best practice or pattern or whatever. Then I can get it into my head what the terms/jargon mean. Guess I'm more visual. That's why I like Flex.
                                          I'm sure you and other professionals here realize how valuable your advice is; please don't get frustrated when you see me tripping a bit.
                                          • 18. Re: Variable not seeing data on another page
                                            spacehog Level 1
                                            Amy

                                            No offense here, but you allude to a FAQ that I (assuming I am the OP) have no idea what you are talking about:

                                            "I didn't jump in with this because it was originally posted a couple of
                                            hours before I posted my Flex FAQ, which I felt was answer enough it the OP
                                            was observant enough to see it and go read it (Q3)."

                                            I would love to read Q3, but I don't understand how I would have known, from looking at the Flex forums, that you had posted something on another site. Can you please provide a link.
                                            • 19. Re: Variable not seeing data on another page
                                              Level 7

                                              "spacehog" <webforumsuser@macromedia.com> wrote in message
                                              news:ghr9oj$anp$1@forums.macromedia.com...
                                              > Amy
                                              >
                                              > No offense here, but you allude to a FAQ that I (assuming I am the OP)
                                              > have no
                                              > idea what you are talking about:
                                              >
                                              > "I didn't jump in with this because it was originally posted a couple of
                                              > hours before I posted my Flex FAQ, which I felt was answer enough it the
                                              > OP
                                              > was observant enough to see it and go read it (Q3)."
                                              >
                                              > I would love to read Q3, but I don't understand how I would have known,
                                              > from
                                              > looking at the Flex forums, that you had posted something on another site.
                                              > Can
                                              > you please provide a link.

                                              I didn't post it at another site. I posted it here, as I do each and every
                                              week, so that people can read the FAQ and find the answers to their own
                                              questions. I can only assume, since the incidence of the questions covered
                                              in the FAQ has gone down since I started doing this, that others are seeing
                                              the posting fine.

                                              For your edification:
                                              http://tinyurl.com/FAQposting


                                              • 20. Re: Variable not seeing data on another page
                                                spacehog Level 1
                                                Amy

                                                "I posted it here, as I do each and every week".

                                                I created this thread on the Adobe Flex forums (on Adobe.com). I followed your link and see that you are part of the Google Flex usergroup. I take it that you are able to see posts that originate from the Adobe.com forums. Those of us who are not part of one the many Flex usergroups do not see original postings like your FAQ.

                                                I looked at Q3 from the link you provided and I will revisit the event dispatching

                                                Thanks
                                                • 21. Re: Variable not seeing data on another page
                                                  Level 7

                                                  "spacehog" <webforumsuser@macromedia.com> wrote in message
                                                  news:ghrcta$egj$1@forums.macromedia.com...
                                                  > Amy
                                                  >
                                                  > "I posted it here, as I do each and every week".
                                                  >
                                                  > I created this thread on the Adobe Flex forums (on Adobe.com). I followed
                                                  > your
                                                  > link and see that you are part of the Google Flex usergroup.

                                                  Groups.google.com is a quick way to search many forums, including the ones
                                                  hosted by Adobe. If you learn to use it, you'll find you can answer a lot
                                                  of your questions without having to ask on here and wait hours or days for a
                                                  reply.

                                                  > I take it that
                                                  > you are able to see posts that originate from the Adobe.com forums.

                                                  Well, yes, because I'm subscribed to macromedia.flex.general_discussion via
                                                  nntp.

                                                  >Those of
                                                  > us who are not part of one the many Flex usergroups do not see original
                                                  > postings like your FAQ.

                                                  I apologize. It does look like there's a disconnect between the nntp side
                                                  and the web side. Here's a hint for future reference: the more advanced
                                                  coders tend to have less patience with slow, clunky web interfaces, so in
                                                  cases where there is a disconnect between the web side and the nntp side, as
                                                  apparently there is here, you are better off posting via nntp to get the
                                                  benefit of the more advanced users on that side.

                                                  I've known for a while I couldn't see all of the web forum posts, but didn't
                                                  realize that the web forum couldn't see all of mine. Thanks for letting me
                                                  know.

                                                  -Amy