15 Replies Latest reply on Jul 1, 2009 7:37 AM by hkpadobe

    Retrieving data from Function

    hkpadobe

      Hi All,

       

      I have created a popup window using TitleWindow in a mxml component. I am using a function for some calculations and am storing the results in a variable. How do I make this variable or function available to the main app file so that I can access the data in the variable or function.

      I am currently in my main app file accessing the variable by creating a new instance of the mxml component that contains the TitleWindow, but am getting the error "TypeError: Error #1009: Cannot access a property or method of a null object reference".

       

      Can anyone provide any ideas?

       

      Thanks,

       

      J

        • 1. Re: Retrieving data from Function
          sri harsha namala Level 2

          Hi,

                     

                             Here you are creating an Instance to the Component ,so every time a new object is creating ,by default the value is null because you didnt assigned value to the Object.

                             I hope this way you can try

                              a)  create an instance ot the Class (In your Case the Popup)

                              b)  crete a static function that will return an object of Type your Popup(i.e You wll return the earlier variable)

                              

                                     once go through the following code Save file as SampleComp.mxml;

           

                             

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

           

           

           

           

           

           

           

           

           

           

           

          <mx:Canvas

           

           

          xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" creationComplete="compleated()"

          >

           

           

          <mx:Script>

          <![CDATA[

           

           

          private static var

          instance:SampleComp;

           

           

          public var

          i:int;

           

           

          private function compleated():

          void

          {

          instance=

           

          this

          ;

          i=100;

          }

           

           

          public static function

          getInstance():SampleComp

          {

           

           

          if(instance==null

          )

          {

          instance=

           

          new

          SampleComp();

          }

           

           

          return

          instance;

          }

          ]]>

           

           

          </mx:Script>

          </mx:Canvas>

          • 2. Re: Retrieving data from Function
            hkpadobe Level 1

            Hi Namala,

             

            From your advice this is what I should try:

             

            In my mxml component file where I have my TitleWindow thats creates the Popup, I should add the code you have given. Is this correct?

             

            Thanks,

             

            J

            • 3. Re: Retrieving data from Function
              hkpadobe Level 1

              I tried adding the code first to my mxml component file , but got same error as before.

              I then tried adding the code to my main app file, but once again got the same error as before:

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

               

              Any other ideas from anyone?

               

              Thanks,

               

              J

              • 4. Re: Retrieving data from Function
                m..... Level 1

                In your main app when you open your new window, you could do something like

                 

                var newWindow:TitleWindow = new TitleWindow();
                PopUpManager.addPopUp(newWindow);
                

                 

                Then you could access any properties of that window from your main app.

                newWindow.dataString
                

                 

                Make sure your variables are public so you have access to them.

                 

                 

                 

                Hope that helps.

                • 5. Re: Retrieving data from Function
                  hkpadobe Level 1

                  Hi,

                   

                  I of course already have the following code in my main app file to display the popup window:

                  public function showEmailPopUp():void {

                   

                  // Create a PopUp Window

                  var popWindow:winPopUp =

                  PopUpManager.createPopUp(this, winPopUp, true) as winPopUp;

                   

                  }

                   

                   

                   



                  I tried using the variable popWindow to try and access the variables used in winPopUp, but no luck. Is what you have adviced the same as this or is it different?

                   

                  Thanks,

                   

                  J

                  • 6. Re: Retrieving data from Function
                    m..... Level 1

                    I'm not sure without seeing your code.

                    In my example I do things slightly differently, I instantiate the window first and then use the addpopup method rather than create, but I wouldn't imagine that'd make a difference.

                     

                    I'm assuming then that the variables you're trying to access in winPopUp are global? What happens when you try to access a variable from the popWindow object?

                    1 person found this helpful
                    • 7. Re: Retrieving data from Function
                      m..... Level 1

                      On 2nd thought, if you do it more like my example it should solve your problem.

                      Since CreatePopUp returns a flex display object (I think), it won't give you the variables you want.

                       

                       

                      So try

                      var popWindow:winPopUp = new winPopUp();
                      PopUpManager.AddPopUp(popWindow);
                      

                      • 8. Re: Retrieving data from Function
                        hkpadobe Level 1

                        When I try and access the variable, I don't get the error I was getting before, but the data being retuened into the variable is null. Now I have checked the variable in the popWindow object and it does have data in it, but when accessing it from the main app file Flex seems to make it null. I'll try your other suggustion.

                         

                        Thanks,

                         

                        J

                        • 9. Re: Retrieving data from Function
                          hkpadobe Level 1

                          Using PopUpManager.addPopUp(popWindow); gives an error in that I need to supply a 2nd argument of type DisplayObject. Not sure what this would be?

                           

                          Thanks,

                           

                          J

                          • 10. Re: Retrieving data from Function
                            m..... Level 1

                            The 2nd argument is the parent display, so try using this and you could also set modal to true if you wanted.

                            ie.

                             

                            PopUpManager.addPopUp(popWindow, this, true);
                            
                            
                            1 person found this helpful
                            • 11. Re: Retrieving data from Function
                              hkpadobe Level 1

                              Using the following:

                              public

                               

                               

                              // Create a PopUp Window

                               

                              PopUpManager.addPopUp(popWindow,

                              this);

                               

                              }

                               

                               

                              function showPopUp():void {

                               

                               


                              When I click on my button that calls the above method I get the following error:

                               

                              TypeError: Error #1009: Cannot access a property or method of a null object reference.
                              at mx.managers::PopUpManagerImpl/addPopUp()[E:\dev\flex_201_borneo\sdk\frameworks\mx\manager s\PopUpManagerImpl.as:226]
                              at mx.managers::PopUpManager$/addPopUp()[E:\dev\flex_201_borneo\sdk\frameworks\mx\managers\P opUpManager.as:148]

                               

                              This was the same as before.

                               

                              The current setup I have at present is that I have an mxml component file(winPopUp) with a TitleWindow that pops up when a button is clicked displaying a datagrid with RadioButtons (using an itemRenderer) in the 1st column with 2 other columns.

                               

                              What I wanted to do was when the User clicked on the RadioButton the selected rows cells are copied into 2 textinput boxes.

                               

                              Currently in the winPopup I am able to get the data of which RadioButton is clicked (using the datagrid.selectedIndex) and the associated cells in the row, but having problems trying to copy this data into the textinput boxes which are in another mxml component(setUpComp). So I can see the data in my winPopUp component, when I try to access the setUpComp in the main app file to copy the data to, it gives the error as above. When I try to access the setUpComp in the winPopUp, I again get the same error as above.

                               

                              Is there a better way of geting the data from a datagrid when a radiobutton is clicked?

                               

                              Thanks,

                               

                              J

                              • 12. Re: Retrieving data from Function
                                m..... Level 1

                                Are you initialising the popWindow variable correctly?

                                • 13. Re: Retrieving data from Function
                                  hkpadobe Level 1

                                  I have created the variable popWindow as follows:

                                   

                                  [Bindable]

                                  public var popWindow:winPopUp();

                                   

                                  But realised it should be

                                   

                                  [Bindable]

                                  public var popWindow:winPopUp() = new winPopUp();

                                   

                                  Doing this gets rid of the error and I can get data by using popWindow.dataString in my main app file.

                                   

                                  So this is better than before, Thanks Bud.

                                   

                                  Do you know if there is a way for me to check when the User clicks on the Close(X) of the TitleWindow, so that I can fire a method to populate the textinput boxes with the RadioButton selected?

                                   

                                  J

                                  • 14. Re: Retrieving data from Function
                                    m..... Level 1

                                    Yeah, in your TitleWindow define close="closeEvent(event)"

                                    <mx:TitleWindow close="closeWindow(event)" />
                                    

                                    And then you can have a function like this, where you could either dispatch an event, or call another function etc... to populate the text boxes.

                                     

                                    private function closeWindow(ev:Event):void
                                    {
                                         PopUpManager.removePopUp(this);
                                    }
                                    

                                    • 15. Re: Retrieving data from Function
                                      hkpadobe Level 1

                                      Yeah! nice one I have got it working. Your info on using the popWindow variable to get access to the data in the winPopUp was the key.

                                       

                                      Thanks very much. I've given feedback for your help.

                                       

                                      J