9 Replies Latest reply on Dec 10, 2009 9:34 AM by paul.williams

    How do I get rid of this error w/components?

    Jerry62712 Level 1

      I broke the application into pieces with a separate file for each of the action script areas and included them in the main mxml.  I then broke out the various forms that appeared in various states into components.  Now when I try to switch to a state and bring in the form, I get this error:


      TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at Forms::DischargeTracking()[C:\Flex Builder 3\DDTransition\DDTran\src\Forms\DischargeTracking.mxml:6]
          at DDTran/_DDTran_DischargeTracking1_c()[C:\Flex Builder 3\DDTransition\DDTran\src\DDTran.mxml:299]
          at mx.core::DeferredInstanceFromFunction/getInstance()[E:\dev\3.1.0\frameworks\projects\fram ework\src\mx\core\DeferredInstanceFromFunction.as:81]
          at mx.states::AddChild/createInstance()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\st ates\AddChild.as:335]
          at mx.states::AddChild/initialize()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\states \AddChild.as:347]
          at mx.states::State/http://www.adobe.com/2006/flex/mx/internal::initialize()[E:\dev\3.1.0\frameworks\projects\ framework\src\mx\states\State.as:199]
          at mx.core::UIComponent/initializeState()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\ core\UIComponent.as:7321]
          at mx.core::UIComponent/commitCurrentState()[E:\dev\3.1.0\frameworks\projects\framework\src\ mx\core\UIComponent.as:7099]
          at mx.core::UIComponent/setCurrentState()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\ core\UIComponent.as:7053]
          at mx.core::UIComponent/set currentState()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:4339 ]
          at DDTran/processSelectedRecord()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\ButtonHandlers.as:141]
          at DDTran/__processSelectedRecordBtn_click()[C:\Flex Builder 3\DDTransition\DDTran\src\DDTran.mxml:472]

       

      ButtonHandler 141:

      private function processSelectedRecord():void
      {
          XMLRecord = XML(dgSearchResults.selectedItem.valueOf().toString());

       

          dischargeTrackingStateBtn.enabled = true;
          dischargeFollowupStateBtn.enabled = true;
          followUpStateBtn.enabled = true;
          guardianStateBtn.enabled = true;

       

      141==>   currentState = "DischargeTracking";
          populateDT();
      }

       

      States set:

      <mx:states>
      <!-- ************************************************************************ -->
      <!-- Discharge Tracking form/state (src/Forms/DischargeTracking.mxml) ******* -->
      <!-- ************************************************************************ -->
          <mx:State name="DischargeTracking">
      299==>        <mx:AddChild position="lastChild">
                  <ns1:DischargeTracking x="37" y="90"/>
              </mx:AddChild>
          </mx:State>

       

      DischargeTracking.mxml (I tried moving the script to the bottom with the error showing up on the new line):

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
          borderStyle="outset" borderThickness="2" width="780">
      <mx:Script>
          <![CDATA[
      6==>        [Bindable] public var FIRSTNAME:String = FirstName.text;
              [Bindable] public var LASTNAME:String = LastName.text;

        • 1. Re: How do I get rid of this error w/components?
          paul.williams Level 4

          I'm guessing FirstName and LastName are TextInput controls inside DischargeTracking.mxml. These controls will be created after the DischargeTracking component is instantiated but before it is displayed. You are getting the error because you are accessing them before they have been created (when they are still null).

           

          There are various possible solutions:

           

          1) Add an initialize event handler to DischargeTracking and update FIRSTNAME and LASTNAME from within it:

           

          <mx:Canvas

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

               borderStyle="outset"

               borderThickness="2"

               width="780"

               initialize="onInitialize()">

               <mx:Script>

                    <![CDATA[

                         [Bindable] public var FIRSTNAME:String;

                         [Bindable] public var LASTNAME:String;

           

           

                         private function onInitialize() : void

                         {

                              FIRSTNAME = FirstName.text;

                              LASTNAME = LastName.text;

                         }

                    ...

               ...

           

          2) Update FIRSTNAME and LASTNAME from the change or valueCommit event of the FirstName and LastName controls respectively, eg:

           

               <TextInput

                    id="FirstName"

                    change="FIRSTNAME = FirstName.text"/>

           

          3) Use a binding tag:

           

               <mx:Binding source="FirstName.text" destination="FIRSTNAME"/>

          • 2. Re: How do I get rid of this error w/components?
            Jerry62712 Level 1

            Thanks for the reply.

             

            Yes, FirstName is an item in the component.  I tried changing the thing around to this (component):

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
                borderStyle="outset" borderThickness="2" width="780">
            <mx:Script>
                <![CDATA[
                    [Bindable] public var FIRSTNAME:String;
            ...

                        <mx:FormItem id="dt_FirstName"
                            label="First Name"
                            x="5" y="5"
                            indicatorGap="5"
                            labelStyleName="labelRight"
                            labelWidth="130">
                            <mx:TextInput id="FirstName"
                                text="{FIRSTNAME}"
                                width="140"
                                maxChars="50"
                                tabIndex="11"/>
                        </mx:FormItem>

             

            So the variable (where I was dying) is bound to the field.  Then in the main mxml I changed it to this:

            <mx:states>

                <mx:State name="DischargeTracking">
                    <mx:AddChild position="lastChild">
                        <ns1:DischargeTracking
                            FIRSTNAME = ""

            ...

             

            This gives me an error in this script:

            private function populateDT():void
            {
            ...

               Application.application.CONTACTNAME.text = XMLRecord.CONTACTNAME;

            at the first assignment.  What I'm doing is taking data from an XMLRecord and putting it on the form.  The error:

            ReferenceError: Error #1069: Property CONTACTNAME not found on DDTran and there is no default value.
                at DDTran/populateDT()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\Utility.as:196]
                at DDTran/processSelectedRecord()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\ButtonHandlers.as:142]
                at DDTran/__processSelectedRecordBtn_click()[C:\Flex Builder 3\DDTransition\DDTran\src\DDTran.mxml:500]

            • 3. Re: How do I get rid of this error w/components?
              paul.williams Level 4

              The binding you've added is one-way, which means changing the text in FirstName won't cause FIRSTNAME to be updated. This maybe what you want - just mentioning it in case you weren't aware.

               

              The other error is because class DDTran doesn't have a CONTACTNAME property. What is DDTran?

              • 4. Re: How do I get rid of this error w/components?
                Jerry62712 Level 1

                DDTran is the main application.  When I go to the DischargeTracking I try to reference the TextInput item.

                 

                DDTran:

                <mx:states>
                    <mx:State name="DischargeTracking">
                        <mx:AddChild position="lastChild">
                            <ns1:DischargeTracking
                                CONTACTNAME = ""
                ...

                CONTRACTNAME will be assigned data in a script.

                 

                DischargeTracking:

                ?xml version="1.0" encoding="utf-8"?>
                <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
                    borderStyle="outset" borderThickness="2" width="780">
                <mx:Script>
                    <![CDATA[
                        [Bindable] public var FIRSTNAME:String;
                ...

                   ]]>
                </mx:Script>

                 

                    <mx:Form id="dtForm"
                        horizontalCenter="0" >

                ...

                         <mx:FormItem id="dt_FirstName"

                                label="First Name"

                                x="5" y="5"
                                indicatorGap="5"
                                labelStyleName="labelRight"
                                labelWidth="130">
                                <mx:TextInput id="FirstName"
                                    text="{FIRSTNAME}"
                                    width="140"
                                    maxChars="50"
                                    tabIndex="11"/>
                            </mx:FormItem>

                 

                <No errors up to his point> Utility script:

                private function populateDT():void
                {
                    if(booIsNewRecord)
                    {
                        dtResetForm();
                    }
                    else
                    {
                //    Application.application.CURRENTCENSUS.text = recCount.toString();
                    Application.application.DischargeTracking.CURRENTCENSUS = "1";
                    DischargeTracking.CURRENTCENSUS = "1";
                All the above causes an error

                • 5. Re: How do I get rid of this error w/components?
                  paul.williams Level 4

                  It isn't clear what the CONTACTNAME or CURRENTCENSUS properties are or where they are defined.

                  • 6. Re: How do I get rid of this error w/components?
                    Jerry62712 Level 1

                    Both those are public variables in the component.  They are bound to TextInput items in that component.

                     

                    When that component is added to the main mxml file, it is with the public variable included in the namespace:

                     

                                <ns1:DischargeTracking
                                    CONTACTNAME = ""

                    where "ns1" is the component folder, DischargeTracking the file name and CONTRACTNAME is a public variable in a script block in the component and is bound to a TextInput on that component.  I used this format once before adding a variable to the (in this case) CONTACTNAME="{some variable in the main mxml}"

                     

                    Thanks for looking at this.  I'm under a ton of pressure to get this working.

                    • 7. Re: How do I get rid of this error w/components?
                      paul.williams Level 4

                      Ok, so you are trying to access controls inside your sub-component via Application.application from your utility script (which I presume is a separate as file)

                       

                      When you add your DischargeTracking child you aren't giving it an id. So it cannot be accessed as a property of Application.application. If you set id="dischargeTracking" then you should be able to access it using "Application.application.dischargeTracking", assuming your application is in the right state.

                       

                      You should then be able to access sub-properties of your DischargeTracking component, eg:

                       

                           Application.application.dischargeTracking.FirstName.text

                           Application.application.dischargeTracking.FIRSTNAME

                       

                      Since your children are being added dynamically it may be wise to add a null check in your utility script:

                       

                      private function populateDT():void
                      {
                           ...

                           if ( Application.application.dischargeTracking != null )

                           {

                                Application.application.dischargeTracking.FirstName.text = "Bob";

                           }

                      }

                       

                      There are arguably better ways of doing all of this rather than using Application.application, but it sounds like now's not the time get into that discussion.

                      • 8. Re: How do I get rid of this error w/components?
                        Jerry62712 Level 1

                        ReferenceError: Error #1069: Property DischargeTracking not found on DDTran and there is no default value.
                            at DDTran/populateDT()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\Utility.as:197]
                            at DDTran/processSelectedRecord()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\ButtonHandlers.as:142]
                            at DDTran/__processSelectedRecordBtn_click()[C:\Flex Builder 3\DDTransition\DDTran\src\DDTran.mxml:536]
                        w/    Application.application.DischargeTracking.CURRENTCENSUS = recCount.toString();

                         

                        ReferenceError: Error #1069: Property CURRENTCENSUS not found on DDTran and there is no default value.
                            at DDTran/populateDT()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\Utility.as:198]
                            at DDTran/processSelectedRecord()[C:\Flex Builder 3\DDTransition\DDTran\src\ASFiles\ButtonHandlers.as:142]
                            at DDTran/__processSelectedRecordBtn_click()[C:\Flex Builder 3\DDTransition\DDTran\src\DDTran.mxml:536]
                        w/    Application.application.CURRENTCENSUS.text = recCount.toString();

                         

                        1119: Access of possibly undefined property CURRENTCENSUS through a reference with static
                        type Class. <syntax error in IDE>
                        w/    DischargeTracking.CURRENTCENSUS.text = recCount.toString();

                         

                        There are two references, one to set the variable equal to either the above or a member in XMLRecord and the other to blank it out.

                         

                        In both cases, I'm refering to the public variable that is bound to the input item.  That public variable is set when the conponent is created.

                        • 9. Re: How do I get rid of this error w/components?
                          paul.williams Level 4

                          Everytime I suggest something you reply with more stack-traces and code samples without any mention of my previous suggestion. Its very hard to keep track of what you are doing or whether you are paying any attention to my replies.