11 Replies Latest reply on May 26, 2009 11:45 AM by Benji2008

    accessing components vars.

    rcanulla Level 1

      Hi,

       

      How do I access a var in a differint component? I have two custom components in the same application and need to get the variable (arrayCollection) from one to populate a chart in the other.

        • 1. Re: accessing components vars.
          Gregory Lafrance Level 6

          In the main app:

           

          var myComp1:Comp1 = new Comp1();

          var myComp2:Comp2 = new Comp2();

           

          In Comp1:

           

          private var myAC:ArrayCollection = mx.core.Application.application.myComp2.arrColl;

           

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

          1 person found this helpful
          • 2. Re: accessing components vars.
            rcanulla Level 1

            Hey man, im getting an error.

             

            main app

                      public var myListItems:listItems = new listItems();
                      public var myCurrentArchiveChart:currentArchiveChart = new currentArchiveChart();
                      
            
            

             

            charting

                    [Bindable]
                   private var mynewsDB:ArrayCollection = mx.core.Application.application.myListItems.newsDB as ArrayCollection;
            

            error

             

            TypeError: Error #1009: Cannot access a property or method of a null object reference.
                 at components::currentArchiveChart()[/Volumes/Files/Canulla/Brodeur/prof_dev/mediaTrapDesk/src/components/currentArchiveChart.mxml:15]
                 at mediaTrapDesk()[/Volumes/Files/Canulla/Brodeur/prof_dev/mediaTrapDesk/src/mediaTrapDesk.mxml:15]
                 at _mediaTrapDesk_mx_managers_SystemManager/create()
                 at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3188]
                 at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3064]
                 at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2916]
            
            

            Thanks in advance for any help!

            • 3. Re: accessing components vars.
              Michael Borbor Level 4

              debug this line of code to see if there's something in this var or not

               

              mynewsDB:ArrayCollection = mx.core.Application.application.myListItems.newsDB 

              Probably you're trying to reference this AC too early in the app life cycle.

              1 person found this helpful
              • 4. Re: accessing components vars.
                rcanulla Level 1

                Nothing in there... How should I proceed to create the var once the other is created?

                 

                Thanks for your help!

                • 5. Re: accessing components vars.
                  Michael Borbor Level 4

                  In your charting component I'll change

                   private var mynewsDB

                  to

                   public var mynewsDB

                  And so in your main app right after you declare the new instance of the component you'll be able to set the mynewsDB var

                   

                  public var myCurrentArchiveChart:currentArchiveChart = new currentArchiveChart();
                  myCurrentArchiveChart.mynewsDB=whateeverAC;
                  • 6. Re: accessing components vars.
                    run,ryan! Level 3

                    I believe its

                    Application.application.root.myListItems.newsDB
                    • 7. Re: accessing components vars.
                      rcanulla Level 1

                      Sorry, but I still can't get this going... Can someone help please?

                       

                      I've declared this in my main app.

                       

                           <mx:Script>
                                <![CDATA[
                                     import mx.core.Application;
                                     import mx.collections.ArrayCollection;
                      
                                     [Bindable]
                                     public var newsDB:ArrayCollection = new ArrayCollection( [
                                       { title: null, clickURL: null, info: null, abstract: null, category: null, rateItem: null }
                                       ]);;
                                     public var myListItems:listItems = new listItems();
                                     public var myCurrentArchiveChart:currentArchiveChart = new currentArchiveChart();
                                     
                                     
                                     //myListItems.newsDB = mx.core.Application.application.newsDB as ArrayCollection;
                                     
                                     //public var myCurrentArchiveChart:currentArchiveChart = new currentArchiveChart();
                                          //myCurrentArchiveChart.mynewsDB=whateeverAC;
                                     
                                     
                                ]]>
                           </mx:Script>
                      

                       

                      and in my chart component

                       

                             [Bindable]
                             public var newsDB:ArrayCollection = mx.core.Application.application.newsDB as ArrayCollection;
                      
                      

                       


                      as well as in my listItems component

                       

                              [Bindable]
                              public var newsDB:ArrayCollection = mx.core.Application.application.newsDB as ArrayCollection;
                      

                       

                      I am still getting this error

                      TypeError: Error #1009: Cannot access a property or method of a null object reference.
                           at components::listItems()[/Users/Ryan/Documents/Flex Builder 3/mediaTrapDesk/src/components/listItems.mxml:65]
                           at mediaTrapDesk()[/Users/Ryan/Documents/Flex Builder 3/mediaTrapDesk/src/mediaTrapDesk.mxml:21]
                           at _mediaTrapDesk_mx_managers_SystemManager/create()
                           at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3188]
                           at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3064]
                           at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2916]
                      
                      
                      • 8. Re: accessing components vars.
                        rcanulla Level 1

                        I should note that I am declaring this in the main app, and populating/assigning it within the two custom components. Any thoughts/help would be much appreciated!

                         

                        Thanks in advance.

                        • 9. Re: accessing components vars.
                          Benji2008 Level 1

                          so you have 2 components on your mxml page ?

                          Cant you just give each of those components an ID - so you can bind the arraycollection from one component to another on that main page ?

                           

                          perhaps it would be easier if you would post the code for that page...

                           

                          I guess the best solution would be, to make a singleton class where you put your arraycollection, that way you can reach it anyplace in your application :-)

                          • 10. Re: accessing components vars.
                            rcanulla Level 1

                            Thanks Benji,

                             

                            ListItems Component

                             

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init(), reloadData()" height="95%" width="100%" backgroundAlpha="0">
                            <mx:Script>
                                 <![CDATA[
                                      import mx.core.Application;
                                      import mx.collections.ArrayCollection;
                                      import mx.containers.Box;
                                      import mx.controls.Alert;
                                      import mx.events.DragEvent;
                                      import flash.net.navigateToURL;
                                      import mx.events.StateChangeEvent;
                                      import mx.controls.ComboBox;
                                      import mx.controls.LinkButton;
                                      import mx.containers.VBox;
                                      import mx.controls.CheckBox;
                                      import mx.controls.Text;
                                      import mx.containers.HBox;
                                      import mx.collections.XMLListCollection;
                                      import mx.rpc.events.ResultEvent;
                                      import mx.managers.CursorManager;
                                      import flash.text.TextField;
                                      
                                      namespace ink = "http://www.inktomi.com/";
                                      use namespace ink;
                                      
                                      //--------------------------------------------------
                                      //
                                      //     Archive Vars
                                      //
                                      // --------------------------------------------------
                                      [Bindable] private var xmlList:XMLList;
                                      [Bindable] private var listCollection:XMLListCollection;
                                      [Bindable] private var listData:XML;
                                      [Bindable] public var serviceURL:String = "http://boss.yahooapis.com/ysearch/news/v1/"; 
                                      [Bindable] public var searchFor:String = "biotechnology%20AND%20biotech";   
                                      private const PROXY:String = "http://localhost/employeePhoneDesktop/src/php/proxy.php?url=";
                                      private const APIID:String = "?appid=######
                                      private const PARAMS:String = "&format=xml&orderby=date";
                                      private var reload:Timer;
                                      
                                      //--------------------------------------------------
                                      //
                                      //     Archive Vars
                                      //
                                      // --------------------------------------------------
                                      private var vBox:VBox;
                                      private var hBox:HBox;
                                      private var titleText:LinkButton;
                                      private var itemInfo:Text;
                                      private var abstract:Text;
                                      private var archive:CheckBox;
                                      private var rateItem:ComboBox;
                                      private var category:ComboBox;
                                      private var container:VBox;
                                      private var clickURL:Text;
                                      private var canvas:Canvas;
                                      private var titleFormat:TextFormat;
                                      
                                      //--------------------------------------------------
                                      //
                                      //     Archive Vars
                                      //
                                      // --------------------------------------------------
                                    
                                      /*[Bindable]
                                      public var newsDB:ArrayCollection = new ArrayCollection( [
                                             { title: null, clickURL: null, info: null, abstract: null, category: null, rateItem: null }
                                             ]);*/
                                             
                                     public var newsDB:ArrayCollection = new ArrayCollection;
                                      
                                      private function init():void {
                                           listService.send()
                                           CursorManager.setBusyCursor();
                                      }
                                      private function getData(e:TimerEvent):void{
                                           listService.send()
                                           CursorManager.setBusyCursor();
                                           removeChild(container);
                                      }
                                      private function reloadData():void {
                                           reload = new Timer(10000, 10);
                                           //reload.start();
                                           reload.addEventListener(TimerEvent.TIMER, getData);
                                           //trace(reload.delay);
                                           
                                           //reload.addEventListener(TimerEvent.TIMER_COMPLETE, 
                                      }
                                      
                                      private function resultHandler(e:ResultEvent):void {
                                           listData = e.result as XML;
                                           // add the imems to an XMLList and XMLListCollection
                                           // so that they can be used with the repeater class
                                           // and update automatically
                                           xmlList = e.result.resultset_news.result as XMLList;
                                           listCollection = new XMLListCollection(xmlList);
                                           CursorManager.removeBusyCursor();
                                           // populate the layout with datd
                                           createLayout();
                                      }
                                      private function createLayout():void {
                                           container = new VBox();
                                           var categoryLabels:Array = new Array("Health","Industrial","Emerging Tech","Food & Ag");
                                           var rateLabels:Array = new Array("Positive","Neutral","Negative");
                            
                                           for(var i:int=0; i<listCollection.length; i++) {
                                                //vBox = new VBox();
                                                canvas = new Canvas();
                                                hBox = new HBox();
                                                titleText = new LinkButton();
                                                titleFormat = new TextFormat();
                                                itemInfo = new Text();
                                                abstract = new Text();
                                                archive = new CheckBox();
                                                rateItem = new ComboBox();
                                                category = new ComboBox();
                                                clickURL = new Text();
                                                
                                                titleText.label = listCollection.getItemAt(i).title;
                                                //titleText.addEventListener(MouseEvent.DOUBLE_CLICK, getURL);
                                                titleText.addEventListener(MouseEvent.CLICK, getURL);
                                                titleText.width = 400;
                                                
                                                clickURL.text = listCollection.getItemAt(i).clickurl;
                                                clickURL.visible = false;
                                                clickURL.includeInLayout = false;
                                                
                                                itemInfo.text = listCollection.getItemAt(i).source + " | " + listCollection.getItemAt(i).date;
                                                itemInfo.y = 25;
                                                abstract.text = listCollection.getItemAt(i).abstract;
                                                abstract.y = 42;
                                                abstract.visible = true;
                                                abstract.includeInLayout = true;
                                                
                                                abstract.width = 400;
                                                abstract.height= 60;;
                                                archive.label = "Archive";
                                                
                                                category.prompt = "Category";
                                                category.dataProvider = categoryLabels;
                                                category.rowCount = categoryLabels.length;
                                                category.visible = false;
                                                category.includeInLayout = false;
                                                category.width = 95;
                                                category.height = 20;
                            
                                                rateItem.prompt = "Rate";
                                                rateItem.dataProvider = rateLabels;
                                                rateItem.visible = false;
                                                rateItem.includeInLayout = false;
                                                rateItem.width = 95;
                                                rateItem.height = 20;
                                                
                                                canvas.addChild(titleText);
                                                canvas.addChild(clickURL);
                                                canvas.addChild(itemInfo);
                                                canvas.addChild(abstract);
                                                canvas.addChild(hBox);
                                                
                                                hBox.addChild(archive);     
                                                hBox.addChild(category);
                                                hBox.addChild(rateItem);
                                                hBox.y = abstract.y + 60;
                                                hBox.percentWidth = 80;
                                                hBox.percentHeight = 80;
                                                archive.addEventListener(Event.CHANGE, toggleArchive);     
                                                               
                                                container.addChild(canvas);          
                                           }
                                           container.x = 10;
                                           container.y = 10;
                                           addChild(container);
                            
                            
                                      }
                               //-------------------------------------------------------------------------
                               //
                               // If someone chooses to archive the item, set the properties of the 
                               // ComboBoxes (visible & includeInLayout) to true
                               // 
                               //-------------------------------------------------------------------------
                               private function toggleArchive(e:Event):void {
                                     var chb:CheckBox = e.currentTarget as CheckBox;
                                     var hBox:HBox = chb.parent as HBox;
                                     var len:int = hBox.numChildren;
                                     for(var i:int=0; i<len; i++) {
                                           var cb:DisplayObject = hBox.getChildAt(i);
                                           if((cb is ComboBox)) {
                                            // toogle visibility and include in layout
                                            (cb as ComboBox).visible = chb.selected;
                                            (cb as ComboBox).includeInLayout = chb.selected;
                                           }
                                     }
                               }
                               //-------------------------------------------------------------------------
                               // Called when someone clicks on the titleLink. This function pulls the 
                               // origional website URL up in a seperate browser window
                               //-------------------------------------------------------------------------
                               private function getURL(e:MouseEvent):void {
                                         var link:LinkButton = e.currentTarget as LinkButton;
                                         var canvas:Canvas = link.parent as Canvas;
                                         var clickURL:Text = canvas.getChildAt(1) as Text;
                                         var url:URLRequest = new URLRequest(clickURL.text);
                                         navigateToURL(url);
                                         
                                      trace(clickURL.text);
                               }
                              /* private function showAbstract(e:MouseEvent):void {
                                            var link:LinkButton = e.currentTarget as LinkButton;
                                         var canvas:Canvas = link.parent as Canvas;
                                         var abstract:Text = canvas.getChildAt(3) as Text;
                                         var hBox:HBox = canvas.getChildAt(4) as HBox;
                                         link.removeEventListener(MouseEvent.CLICK, showAbstract);
                                         link.addEventListener(MouseEvent.CLICK, hideAbstract);
                                         abstract.visible = true;
                                         abstract.includeInLayout = true;
                                         while(abstract.hitTestObject(hBox) == true){
                                              hBox.y += 1;
                                         }
                                         trace(canvas.getChildren());
                                       }
                               private function hideAbstract(e:MouseEvent):void {
                                            var link:LinkButton = e.currentTarget as LinkButton;
                                         var canvas:Canvas = link.parent as Canvas;
                                         var abstract:Text = canvas.getChildAt(3) as Text;
                                         var hBox:HBox = canvas.getChildAt(4) as HBox;
                                         link.removeEventListener(MouseEvent.CLICK, hideAbstract);
                                         link.addEventListener(MouseEvent.CLICK, showAbstract);
                                         abstract.visible = false;
                                         abstract.includeInLayout = false;
                                      hBox.y = 35;
                                         trace(abstract.text);
                               }
                               */
                               public function archiveData():void {
                                         var containerArray:Array = container.getChildren() as Array;
                                         for(var i:int=0;i<containerArray.length;i++){
                                         //for(var i:int=0;i<1;i++){
                                              var canvas:Canvas = container.getChildAt(i) as Canvas;
                                              var titleText:LinkButton = canvas.getChildAt(0) as LinkButton;
                                              var clickURL:Text = canvas.getChildAt(1) as Text;
                                              var itemInfo:Text = canvas.getChildAt(2) as Text;
                                              var abstract:Text = canvas.getChildAt(3) as Text;
                                              var hBox:HBox = canvas.getChildAt(4) as HBox;
                                              var archive:CheckBox = hBox.getChildAt(0) as CheckBox;
                                              var category:ComboBox = hBox.getChildAt(1) as ComboBox;
                                              var rateItem:ComboBox = hBox.getChildAt(2) as ComboBox;     
                                              if(archive.selected == true){
                                                newsDB.addItem( { 
                                                title: titleText.label, 
                                                clickURL: clickURL.text, 
                                                info: itemInfo.text, 
                                                abstract: abstract.text, 
                                                category: category.selectedLabel, 
                                                rateItem: rateItem.selectedItem });                    
                                              }     
                            
                                         }
                                              trace(newsDB[0].title);
                                         
                               }
                               private function archiveSuccess():void {
                                         Alert.show("Your clips have been successfully saved","Archive Complete");        
                               }
                               private function archiveFault():void {
                                         Alert.show("Your clips have not been saved. Please contact an administrator.","Archive Error");
                               }
                                      
                                 ]]>
                            </mx:Script>
                            
                                 <mx:HTTPService id="listService" url="{serviceURL + searchFor + APIID + PARAMS}" result="resultHandler(event)" resultFormat="e4x"/>
                            
                            </mx:Canvas>
                            
                            

                             

                            Main mxml

                             

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
                                 layout="absolute" 
                                 xmlns:com="components.*" 
                                 backgroundGradientAlphas="[1.0, 1.0]" 
                                 backgroundGradientColors="[#EDF4FF, #C6D9FF]" 
                                 width="800" height="600"
                                 backgroundAlpha=".75"
                                 title="  Media Trap Beta V1.1"
                                 titleAlignment="left"
                                 creationComplete="init()">
                                 
                                 <mx:Script>
                                      <![CDATA[
                            
                                           
                                      ]]>
                                 </mx:Script>
                            
                                 
                                 
                                 <mx:Style source="assets/style.css" />
                                 <mx:Label x="19" y="14" text="Media Trap" fontWeight="bold" fontSize="20"/>
                                 <mx:Label x="20" y="38" text="beta 1.1"/>
                                 <mx:TabNavigator id="navigator" width="450" height="89%"  x="20" y="61">
                                      <mx:VBox label="News">
                                           <com:listItems id="newsFeed" name="newsFeed" backgroundColor="#FFFFFF" x="0" y="0" height="100%">
                                           </com:listItems>
                                      </mx:VBox>
                                      <mx:VBox label="Blogs">
                                           <mx:Text text="Blog Coverage"/>
                                      </mx:VBox>
                                      <mx:VBox label="Twitter">
                                           <mx:Text text="Twitter Coverage"/>
                                      </mx:VBox>
                                      <mx:VBox label="Video">
                                           <mx:Text text="Video Coverage"/>
                                      </mx:VBox>
                                 </mx:TabNavigator>
                                 <mx:Button x="348" y="36" label="Archive Selected" click="newsFeed.archiveData()"/>
                                 <!--<com:currentArchiveChart id="currentCats" x="478" y="81" height="212" />-->
                            
                                 
                            </mx:WindowedApplication>
                            

                             

                            Charting Component

                             

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="346" xmlns:com="components.*" creationComplete="init()" >
                                <mx:Script>
                                    <![CDATA[
                                    import mx.core.Application; 
                                    import mx.collections.ArrayCollection;
                            
                                      // testing variable
                                    [Bindable]
                                    private var medalsAC:ArrayCollection = new ArrayCollection( [
                                        { Country: "USA", Gold: 35, Silver:39, Bronze: 29, Test: 20000 }
                                        ]);
                                        
                                            
                                    private function init():void{
                                         trace(mx.core.Application.application.myListItems.newsDB as ArrayCollection);
                                         medalsAC.addItem( { Country: "Canada", Gold: 75, Silver:39, Bronze: 29 });
                                         medalsAC.addItem( { Country: "England", Gold: 100, Silver: 200, Bronze: 100 });
                                         
                                    }
                                    private function displayGold(data:Object, field:String, index:Number, percentValue:Number):String {
                                        var temp:String= (" " + percentValue).substr(0,6);
                                        return data.Country + ": " + '\n' + "Total Gold: " + data.Gold + '\n' + temp + "%";
                                    }
                                    ]]>
                                </mx:Script>
                                
                                
                                     <mx:Label id="archiveTitle" x="10" y="10" text="Current Archive " fontSize="14" fontWeight="bold"/>
                                     <mx:PieChart id="chart" height="130" width="300" color="0x323232"
                                        showDataTips="false" dataProvider="{medalsAC}"  y="22">
                                        
                                        <mx:series>
                                            <mx:PieSeries labelPosition="callout" field="Gold">
                                    
                                            </mx:PieSeries>
                                        </mx:series>
                                    </mx:PieChart>
                            
                            
                            </mx:Canvas>
                            
                            
                            • 11. Re: accessing components vars.
                              Benji2008 Level 1

                              I hope I can help, but I'll try:)

                               

                              So - which arrayCollection from your listItems component do you want to use in your charting component ?