5 Replies Latest reply on Sep 7, 2009 2:13 PM by zakabalt

    Null object error between components

    zakabalt Level 1

      Hi, the other day I asked something similar and I got it, but now I can't do this. I want to execute a function that I have declared in one component doing click in another component button. I will put some code to understand better. I have this code in component1

       

      Component1:

      public function getServi():void {
                  info = Environment.getInstance().
                          serviceLocator.getterService("com.adur.prueba.Conexion");
                  info.addEventListener(ServiceInfoEvent.READY, onInfoResult);
                  info.addEventListener(ServiceInfoEvent.ERROR, onInfoFault);
                  info.lookup();
              }

       

      And I want to execute getServi doing click in one button that I have in Component2:

       

      Component2:

       

      <Script>

      ....

      public var metl:Arbol = new Arbol();

      ...

      </Script>

       

      . . . . . . . .

      . . . . . . . .

       

      <mx:Button icon="@Embed(source='../img/receive.png')" width="50" height="26" enabled="true"
                          labelPlacement="left" click="met.getServi()"/>

       

      What I'm doing wrong?

       

      Thanks!

        • 1. Re: Null object error between components
          Andrew Rosewarn Level 3

          Couple of things

           

          public var metl:Arbol = new Arbol();

          ...

          </Script>

           

          . . . . . . . .

          . . . . . . . .

           

          <mx:Button icon="@Embed(source='../img/receive.png')" width="50" height="26" enabled="true"
                              labelPlacement="left" click="met.getServi()"/>

           

          These aren;t the same variable name

           

          And in the Component whats info??  We don;t see it decared.

           

          Is there a var info:Something = new Something somewhere else.??

           

          That could be your null?

           

          Andrew

          • 2. Re: Null object error between components
            Gregory Lafrance Level 6

            Yeah, you need to ensure nothing is private that would need to ne accessible in the other components.

             

            BTW, it helps us if you can tell us what the error is saying null. I know this can be tough to troubleshoot, but it helps us provide an answer, and along the way you might find the problem.

             

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

            • 3. Re: Null object error between components
              zakabalt Level 1

              I have written wrong the method name here but not in my code, this is not the problem. I have all the methods or variables declarate public.This is the error:

               

              TypeError: Error #1009: No se puede acceder a una propiedad o a un método de una referencia a un objeto nulo.// In english something like this -> Can not access a property or method of a null object reference
                  at comp::Arbol/treeLoadedResult()[C:\Documents and Settings\java\Escritorio\workspace\MailModule\src\comp\Arbol.mxml:70]
                  at flash.events::EventDispatcher/dispatchEventFunction()
                  at flash.events::EventDispatcher/dispatchEvent()
                  at mx.rpc::AbstractService/dispatchEvent()
                  at mx.rpc.remoting.mxml::RemoteObject/dispatchEvent()
                  at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
                  at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()
                  at mx.rpc::Responder/result()
                  at mx.rpc::AsyncRequest/acknowledge()
                  at NetConnectionMessageResponder/resultHandler()
                  at mx.messaging::MessageResponder/result()

               

               

               

               

               

              I will put more code for you:

               

              Inbox.mxml (Component1):

               

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

              <mx:Script>
                  <![CDATA[
                     
                      public var arbol:Arbol = new Arbol();
                  ]]>
              </mx:Script>

                  <mx:HBox width="100%" height="100%" verticalAlign="middle">
                      <mx:Spacer width="25" height="17"/>
                          <mx:HBox>
                              <mx:Button icon="@Embed(source='../img/recibir.png')" width="50" height="26" enabled="true"
                                  labelPlacement="left" click="arbol.getServi()"/>
                          </mx:HBox>

                 </mx:HBox>

              </mx:canvas>

               

              Arbol.mxml(Component2):

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="240" height="400"
                  creationComplete="getServi()">
                 
                 
                  <mx:Script>
                  <![CDATA[
                     
                      import mx.controls.Tree;
                      import mx.controls.Alert;
                      import mx.events.ListEvent;
                      import mx.events.TreeEvent;
                      import mx.rpc.events.ResultEvent;
                      import mx.rpc.events.FaultEvent;

                      import com.ad.services.event.ServiceInfoEvent;
                      import com.ad.services.IServiceInfo;
                      import com.ad.services.Service;
                      import com.ad.system.Environment;
                         
                      public var info:IServiceInfo;
                     
                      [Embed(source="img/folder.png")]
                        public var ico:Class;
                          
                      public var newTree:Tree = new Tree();
                      public var xmlTree:XML = new XML();
                      public var selectedNode:XML;
                      public var accountId:Number = 1;
                                     
                      public function getServi():void {
                          info = Environment.getInstance().
                                  serviceLocator.getterService("com.adur.prueba.Conexion");
                          info.addEventListener(ServiceInfoEvent.READY, onInfoResult);
                          info.addEventListener(ServiceInfoEvent.ERROR, onInfoFault);
                          info.lookup();
                      }
                     
                      public function onInfoResult(event:Event):void{
                          info.removeEventListener(ServiceInfoEvent.READY, onInfoResult);
                          info.removeEventListener(ServiceInfoEvent.ERROR, onInfoFault);
                          var  ser:Service = info.getService();
                          ser.addEventListener(ResultEvent.RESULT, treeLoadedResult);
                          ser.addEventListener(FaultEvent.FAULT, treeLoadedError);
                             
                          ser.getFolder(Accountid);
                      }
                     
                      //El servicio no existe o se tiene permiso
                      public function onInfoFault(event:Event):void{
                          info.removeEventListener(ServiceInfoEvent.READY, onInfoResult);
                          info.removeEventListener(ServiceInfoEvent.ERROR, onInfoFault);
                          Environment.getInstance().showInfoUser("Service error");
                      }
                                
                      public function treeLoadedResult(event:ResultEvent):void {
                            xmlTree = XML(event.result.toString());
                            var lista:XMLList= xmlTree.Cuenta;
                            for(var i:Number=0;i<lista.length(); i++)
                            {
                                 var tmpTree:XMLList = xmlTree.Cuenta[i].node;
                                newTree.dataProvider = tmpTree;
                                newTree.labelFunction = tree_labelFunc;
                                newTree.addEventListener(ListEvent.ITEM_CLICK,openTree);
                                treePanel.addChild(newTree);
                           }                            
                         }
                            
                         public function treeLoadedError(event:FaultEvent):void{
                          lbl.text = "Error : " + event.message;
                      }
                             
                     
                      //How many new mails (not definetly)
                      public function tree_labelFunc(xmlTree:XML):String {
                          var label:String;
                          label = xmlTree.@label + '(' + 'Nuevos' + ')';
                          return label;
                      }
                     
                   
                      //Open the tree
                      public function openTree(event:ListEvent):void {
                           var node:XML = Tree(event.target).selectedItem as XML;
                           var isOpen:Boolean = newTree.isItemOpen(node);
                           newTree.expandItem(node,!isOpen);
                         }
                       }

                      
                   ]]>
                  </mx:Script>

                  <mx:VBox id="treePanel" width="240" height="400" x="0" y="0">
                  </mx:VBox>
                  <mx:Label id="lbl"/>

              ........(I have more code but isn't important to this question)

              </mx:Canvas>

               

              I want to do click in the first component button and execute getServi().

               

              And another question, if I want to execute getServi, all the methods that execute getServi must be public, or only must be public getServi() and the variables that I have in this method?

              • 4. Re: Null object error between components
                TheFlexGuy Level 2

                Functions that you wish to execute from outside the file must be public, so just getServi needs to be public.

                 

                If the two components are children of the main application, your main application should be listening for the click event from Inbox.myButton (assuming you give the button an id of myButton).  Then the main application calls the getServi method in the click event handler.

                 

                -Marty [ http://www.theflexguy.com ]

                Helpful/Answered? Please mark it as such.

                • 5. Re: Null object error between components
                  zakabalt Level 1

                  But if I'm not the responsable to the main aplication? I only have to do some components and in this case I have to do these components and the conection between they,