6 Replies Latest reply on Sep 1, 2009 4:54 AM by Barna Biro

    Error null object

    zakabalt

      Hello, I have two components, and I want to initialize one variable in one component and do variable++ in the other component and trace the number. These are the two components:

       

      MyComponent:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="150" height="35">

         <mx:Script>

            <![CDATA[

              public var _idAccount:Number=1;

            ]]>

         </mx:Script>

      </mx:Canvas>

       

      MyComponent2:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100" height="20" xmlns:comp="comp.*">

      <mx:Script>
              <![CDATA[
         
              public var mc:MyComponent;

                public var number:Number = mc._idAccount;
         
              public function plus():void {

                     trace(number);

                     number++;

                 
            }
              ]]>
      </mx:Script>
         
          <mx:Button id="button" label="++" click="plus()" width="100%" height="100%"/>
         
      </mx:Canvas>

       

      It don't have to be exactly like in my code, if you have a better sugestion better for me.

       

      Thanks!

        • 1. Re: Error null object
          Barna Biro Level 3

          You are getting the error message because you have never instantiated the component. public var mc:MyComponent; should be public var mc:MyComponent = new MyComponent();

           

          With best regards,

          Barna Biro

          • 2. Re: Error null object
            zakabalt Level 1

            Yes, thanks. You're right!

             

            Arrrghhhh stupid error..

            • 3. Re: Error null object
              Andrew Rosewarn

              You could do the increment in Component 1

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="294" height="222" xmlns:ns1="*">
                  <mx:Script>
                      <![CDATA[
                          [Bindable]
                          private var _number:int;
                         
                          private function incNumber():void {
                              _number ++;
                          }
                      ]]>
                  </mx:Script>
                  <mx:Label x="10" y="10" text="{_number.toString()}" width="144"/>
                  <ns1:Comp2 x="21.5" y="75" width="251" height="118" numberInc="incNumber()">
                  </ns1:Comp2>
              </mx:Canvas>

               

              And listen for an event from Component 2 telling the increment to take place,

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" xmlns:ns1="*">
                  <mx:Script>
                      <![CDATA[
                          private function incNumber():void {
                              dispatchEvent(new Event('numberInc'))
                          }
                      ]]>
                  </mx:Script>
                 
                  <mx:Metadata>
                      [Event(name="numberInc", type="flash.events.Event")]
                  </mx:Metadata>
                 
                  <mx:Button x="22" y="28" label="Inc Number" click="incNumber()"/>

               

              </mx:Canvas>

               

              That was Comp1 doesn;t have to reach into Comp 2 for the number.  Obviously mine is instansiated in mxml, but if in code then yes as Brana said you need to instansiate it with the new keyword, and *** the event listener.

               

              Andrew

              • 4. Re: Error null object
                Barna Biro Level 3

                Sorry to rush in with an opinion but I think that dispatching events for such operations will just create an additional overhead and it brings nothing good to the application ( overall ). The most straight forward solution would be to define a public function in Component1 called "incrementValue" ( or whatever you prefer ) that once called, would increment a private variable from within the component ( or whatever value you want ). Then, define a public getter that would return the current value of the incremented object when needed.

                 

                There's really no point in using events for something like this, it would only create an additional and unwanted overhead.

                 

                Just my 2 cents.

                Barna Biro

                • 5. Re: Error null object
                  Andrew Rosewarn Level 3

                  Yes I guess that makes sense.  I guess I was thinking more along then lines of one incremental control dispatching an event, that any number of other components would listen for.  However thinking about that it would make sense if an event was used to dispatch the newly incremented number inside the event so every other comp could just pick up the new value.

                   

                  Hmm good point Barna, thats one good thing about these forums. lots of other ideas to learn from and when to realise I may be over complicating it for some requirements.

                   

                  Andrew

                  • 6. Re: Error null object
                    Barna Biro Level 3

                    You're welcome. If you want a really clean way ( or at least, much cleaner than the ones mentioned ) that would allow other components to react whenever a property changes ( or any other change ) then read up on the Observer Pattern ( if you are not already familiar with it ). Implementing a Subscriber/Publisher mechanism is way cleaner and more efficient than dispatching events. We can also take this much higher and implement a full MVC architecture that would not only make use of the observer pattern but other patterns as well to get an efficient and flexible application ( this does not mean that using patterns is always good just like with dispatching events, if the application in case is really small and is not really planned to change then implementing various design patterns would be an overkill - an overkill that would/could bring some "virtual and future benefits", but an overkill non the less ).

                     

                    Cheers,

                    Barna Biro