2 Replies Latest reply on Apr 13, 2010 9:13 PM by Vikash.kumar29

    how to refer mxml in Actionscript class.

    vipin.nemade

      hello friends,

      I have created small application for login in flex desktop application. In which I am refering webservice method for login for this have created the Authentication class. Now I want to refer different  Textinput value for mobile no and Textinput value for password. In my Authentication class.

      for this I have created the object of mxml class.And using this I am getting the mobile no value and password value in My Action script class.

      This my code :-

       

       

      SBTS.mxml file

       

       

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

      <!-- usingas/AccessingPackagedClasses.mxml -->

      <mx:WindowedApplication

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

       

      <mx:Script>

      <![CDATA[

       

       

      public

       

       

       

       

       

      function login():void

      {

       

       

      var User:Authentication;

      User =

      new Authentication();

      User.authentication();

      }

      ]]>

       

      </mx:Script>

       

       

      <mx:Panel width="100%" height="100%" layout="absolute">

       

       

      <mx:TabNavigator width="100%" height="100%" id="viewstack2">

       

       

      <mx:Form label="Login Form" id="loginform">

       

       

      <mx:FormItem label="Mobile no:">

       

       

      <mx:TextInput id="mobileno"/>

       

       

      </mx:FormItem>

       

       

      <mx:FormItem label="Password:">

       

       

      <mx:TextInput displayAsPassword="true" id="password" />

       

       

      </mx:FormItem>

       

       

      <mx:FormItem>

       

       

      <mx:Button label="Login" click="login()"/>

       

       

      </mx:FormItem>

       

       

      </mx:Form>

       

       

      <mx:Form label="Child List">

       

       

      <mx:Label width="100%" color="blue"

      text="

      Select Child."/>

       

       

       

       

       

       

      </mx:Form>

       

      </mx:TabNavigator>

       

       

       

       

       

       

       

      </mx:Panel>

       

      </mx:WindowedApplication>

       

      Action script class :-

      package

       

       

       

       

       

      src

      {

       

      import adobe.utils.XMLUI;

       

       

      import generated.webservices.*;

       

       

      import mx.collections.ArrayCollection;

       

      import mx.controls.Alert;

       

      import mx.rpc.events.FaultEvent;

       

      public class Authentication

      {

      [

      Bindable]

       

      private var childName:ArrayCollection;

      [

      Bindable]

       

      private var childId:ArrayCollection;

       

      private var photoFeed:ArrayCollection;

       

      private var arrayOfchild:Array;

       

      private var newEntry:GetSBTSMobileAuthentication;

       

      public var user:SBTSWebService;

       

      public var mxmlobj:SBTS;

       

      public function authentication():void

      {

       

      user =

       

      new SBTSWebService();

       

       

      if(user!=null)

      {

      user.addSBTSWebServiceFaultEventListener(handleFaults);

      user.addgetSBTSMobileAuthenticationEventListener(authenticationResult);

       

      newEntry =

      new GetSBTSMobileAuthentication();

       

      if(newEntry!=null)

      {

      mxmlobj =

      new SBTS();

       

      if(mxmlobj != null)

      {

      newEntry.mobile = mxmlobj.mobileno.text; // Getting error here   error mention below

        newEntry.password= mxmlobj.password.text;

      }

      user.getSBTSMobileAuthentication(newEntry);

      }

      }

       

      }

       

      public function handleFaults(event:FaultEvent):void

      {

      Alert.show(

       

      "A fault occured contacting the server. Fault message is: " + event.fault.faultString);

      }

       

      public function authenticationResult(event:GetSBTSMobileAuthenticationResultEvent):void

      {

       

      if(event.result != null && event.result._return>0)

      {

       

       

      if(event.result._return > 0)

      {

       

      var UserId:int = event.result._return;

       

       

      if(mxmlobj != null)

      {

      mxmlobj.loginform.enabled =

      false;

       

      mxmlobj.viewstack2.selectedIndex=1;

      }

      }

       

      else

      {

      Alert.show(

      "Authentication fail");

      }

       

       

      }

       

      }

       

      }

      }

       

      I am getting this error :-

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

      at SBTSBusineesObject::Authentication/authentication()[E:\Users\User1\Documents\Flex Builder 3\SBTS\src\SBTSBusineesObject\Authentication.as:35]

      at SBTS/login()[E:\Users\User1\Documents\Flex Builder 3\SBTS\src\SBTS.mxml:12]

      at SBTS/___SBTS_Button1_click()[E:\Users\User1\Documents\Flex Builder 3\SBTS\src\SBTS.mxml:27]

       

      please help me to remove this error.

       

       

       

       

        • 1. Re: how to refer mxml in Actionscript class.
          radikalFish Level 1

          Hi

           

          You have an instantiation like this

           

          mxmlobj =

          new SBTS();

           

          and immediately you are trying to access one of its childs via

           

          newEntry.mobile = mxmlobj.mobileno.text

           

          this might not been created at this moment

          you have to wait for the "creation_complete" event fired for this component like

           

          mxmlobj.addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete, false, 0, true);

           

          and inside that event handler you can access its childs

           

          protected function onCreationComplete(ev:Event):void {

              newEntry.mobile = mxmlobj.mobileno.text

          }

           

          does this work?

           

          P.S.

          There might spelling errors i ve just typed an answer without testing...

          • 2. Re: how to refer mxml in Actionscript class.
            Vikash.kumar29 Level 3

            You said :

             

             

             

            if(newEntry!=null)

             

            {

             

            mxmlobj =

             

            new SBTS();

             

             

             

            if(mxmlobj != null)

             

            {

             

            newEntry.mobile = mxmlobj.mobileno.text; // Getting error here   error mention below

             

              newEntry.password= mxmlobj.password.text;

             

            }

             

            //------ Your rest of code

            }
            but SBTS is you top WindowApplication mxml object. So saying

             


            mxmlobj = new SBTS();

             


            will create new Instance of SBTS class and not contain your original value shown in UI.

             


            You can do this by saying

             


            <mx:Script>

             

            <![CDATA[

             

             

             

             

             

            public

             


            function login():void

             

            {

             

            var User:Authentication;

             

            User = new Authentication();
            User.authentication(this); // note we have passed reference of SBTS class
            }

             

            ]]>

             

             

             

            </mx:Script>

             

            Now modify your class Authentication like

             

            public function authentication(inSbts:SBTS):void

             

            {

             

            mxmlobj = inSbts // assign the refernce here

             

             

             

            user = new SBTSWebService();

             


            ------------ Your Original Code --------------------------------------

             

             

             

            if(newEntry!=null)

             

            {

             

            //mxmlobj = new SBTS();  no need of doing this as we have reference of window application/UI

             

             

             

            if(mxmlobj != null)

             

            {

             

            newEntry.mobile = mxmlobj.mobileno.text; // Since mxmlObj is already added to stage to will not any error

             

              newEntry.password= mxmlobj.password.text;

             

            }

             

            ---- -------------------Your rest of code -------------------------------------------------

             

             

             

             

             


            Now you are done, and hope this will solve your problem.

             

            Cheers

             

            -Vikash