3 Replies Latest reply on Jan 17, 2007 2:04 PM by Starlover_jacob

    Cannot access a property or method of a null object reference.

    Starlover_jacob Level 1
      hello,

      I have created some nice piece of code in a test project file.
      Now i copied this code in my project.
      When i tried debugging it gives me the following error;

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

      the runtime error site is written the following:
      http://livedocs.macromedia.com/flex/2/langref/runtimeErrors.html

      An object that evaluates to null can have no properties. This error can occur in some unexpected (though valid) situations. For example, consider the following code, which creates a Sprite object. Because this Sprite object is never added to the display list (through the addChild() method of a DisplayObjectContainer object), its stage property is set to null. Thus, the example generates this error because Sprite object's stage property cannot have any properties:
      import flash.display.Sprite;
      var sprite1:Sprite = new Sprite();
      var q:String = sprite1.stage.quality;

      my question:
      How to solve this problem. The code in my testfile works fine.

      Please a solution

      Greetz, Jacob
        • 1. Cannot access a property or method of a null object reference.
          Starlover_jacob Level 1
          add on my question;

          i discoverd that my error is coused by the fact that i put the code inside a tabnavigator tag.

          <mx:TabNavigator x="30.5" y="354" width="323" height="236" horizontalAlign="center" horizontalGap="5" id="tabBar">

          </mx:TabNavigator>
          If i put it outside the tabnav all code works fine.

          this is my code:
          --------------------------------------------------------------
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          layout="absolute"
          height="600"
          width="670"
          backgroundColor="white"
          backgroundGradientColors="[#ffffff, #abadc2]"
          backgroundGradientAlphas="[0.24, 0.24]"
          initialize="init()">
          <mx:Script>
          <![CDATA[
          // initialisatie van de sitemap tree
          private function init():void
          {
          LinkDescription.text="";
          Url.text="";
          }

          ]]>
          </mx:Script>

          <!-- ##########################Begin output################################# -->
          <mx:TabNavigator x="30.5" y="22" width="629.5" height="568" horizontalAlign="center" horizontalGap="5" id="tabBar">
          <mx:Canvas label="Links" width="100%" height="394" id="linkInst">
          <!-- links toevoegen -->
          <mx:Panel width="278" height="206" layout="absolute" borderColor="#abadc2" title="Links Toevoegen" id="hoofdlinkspanel" left="20" top="66">
          <mx:Label x="0" y="5" text="Titel:"/>
          <mx:TextInput x="68" y="3" id="LinkDescription"/>
          <mx:Label x="0" y="35" text="Url:"/>
          <mx:TextInput x="68" y="33" id="Url"/>
          <mx:CheckBox x="68" y="63" label="Is Sublinks" id="IsSublink"/>
          <mx:Button x="174" y="85" label="Save"/>
          <mx:Button id="listRemove" label="Remove from List" x="10" y="134"/>
          <mx:Button x="166" y="134" label="Xml laden"/>
          </mx:Panel>
          <!-- boomstructuur -->
          <mx:Panel x="315" y="10" width="289" height="366" layout="absolute" borderColor="#abadc2" title="Boomstructuur">
          <mx:Tree id="prodTree" width="269" height="100%" dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" showRoot="false" labelField="@name" right="0" top="0"/>
          </mx:Panel>
          </mx:Canvas>
          <mx:Canvas label="whoei" width="100%" height="394" id="whoei2"></mx:Canvas>
          </mx:TabNavigator>
          </mx:Application>
          -------------------------------------------------------------------------------------
          and this my errors
          ------------------------------------------------------------------------------------
          [SWF] C:\flex exercise files\exercises\treetest\bin\treetest-debug.swf - 814.410 bytes after decompression
          TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at treetest/treetest::init()[C:\flex exercise files\exercises\treetest\treetest.mxml:19]
          at treetest/___Application1_initialize()[C:\flex exercise files\exercises\treetest\treetest.mxml:10]
          at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
          at flash.events::EventDispatcher/dispatchEvent()
          at mx.core::UIComponent/dispatchEvent()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UICompone nt.as:8323]
          at mx.core::UIComponent/set processedDescriptors()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UIComponent.as:1143]
          at mx.core::Container/createComponentsFromDescriptors()[C:\dev\flex_201_gmc\sdk\frameworks\m x\core\Container.as:3546]
          at mx.core::Container/mx.core:Container::createChildren()[C:\dev\flex_201_gmc\sdk\frameworks \mx\core\Container.as:2618]
          at mx.core::UIComponent/initialize()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\UIComponent. as:4937]
          at mx.core::Container/initialize()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\Container.as:2 555]
          at mx.core::Application/initialize()[C:\dev\flex_201_gmc\sdk\frameworks\mx\core\Application. as:829]
          at treetest/initialize()[C:\flex exercise files\exercises\treetest\treetest.mxml:0]
          at mx.managers::SystemManager/ http://www.adobe.com/2006/flex/mx/internal::childAdded()[C:\dev\flex_201_gmc\sdk\framework s\mx\managers\SystemManager.as:1553
          at mx.managers::SystemManager/mx.managers:SystemManager::initializeTopLevelWindow()[C:\dev\f lex_201_gmc\sdk\frameworks\mx\managers\SystemManager.as:2330]
          at mx.managers::SystemManager/mx.managers:SystemManager::docFrameHandler()[C:\dev\flex_201_g mc\sdk\frameworks\mx\managers\SystemManager.as:2214]
          -------------------------------------------------------------------------
          I hope you can give me the solution to my problem.

          (i'm sorry for my english.. im from holland)

          Greetz, jacob
          • 2. Cannot access a property or method of a null object reference.
            Flex D Level 1
            Try adding creationPolicy="all" to the tabNavigator tag. As mentioned in another post, by default the TabNavigator only creates the contents of the first child, so if you have a form item in a tab, it is never created.

            Mike DiNapolis
            • 3. Re: Cannot access a property or method of a null object reference.
              Starlover_jacob Level 1
              Ive got the answer from peter ent.

              This is an event-timing problem. You have: initialize="init()"

              The initialize event is called after all objects have been created (that
              is, the new operation has been run on them). Initialize is only the
              first step in Flex. Once objects are created they can then be allowed to
              create their user interfaces.

              You also have a TabNavigator with a child (Canvas) which in turn has a
              child (Panel) which is where the LinkDescription and Url (your
              TextInputs) lie.

              However, TabNavigator, by default, uses deferred instantiation. This
              means that only the first child is created - and not any of its children
              - during the initialize phase. In other words, TabNavigator created the
              Canvas, the Canvas did not create the Panel which means LinkDescription
              and Url did not created. That's why they are null.

              You have 2 choices: you can make TabNavigator create all of its children
              by using creationPolicy="all" on the <mx:TabNavigator> tag. Or you can
              change the initialize event on the Application to creationComplete.

              Using creationComplete means that once ALL of the children have been
              created will the event be fired. This means TabNavigator's first child's
              children, your LinkDescription and Url TextInput fields included.

              Using creationPolicy="all" is OK when TabNavigator isn't complex. But if
              you had 20 tabs and each had lots of components, the start-up time for
              your application would suffer as all of those components would need to
              be made.

              I'd go with creationComplete.


              Peter Ent | Adobe Systems | Flex Product Support | 617 219-2294
              http://weblogs.macromedia.com/pent/

              I solved my problem by using the creationcomplete of the tab.
              like this:
              --------------------------------------------------------------------------------------
              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              layout="absolute"
              height="600"
              width="670"
              backgroundColor="white"
              backgroundGradientColors="[#ffffff, #abadc2]"
              backgroundGradientAlphas="[0.24, 0.24]">
              <mx:Script>
              <![CDATA[
              private function initLinkinst():void
              {
              LinkDescription.text="";
              Url.text="";
              }
              ]]>
              </mx:Script>

              <!-- ##########################Begin output################################# -->
              <mx:TabNavigator x="30.5" y="22" width="629.5" height="568" horizontalAlign="center" horizontalGap="5" id="tabBar">
              <mx:Canvas label="Links" width="100%" height="394" id="linkInst" creationComplete="initLinkinst()">
              <!-- links toevoegen -->
              <mx:Panel width="278" height="206" layout="absolute" borderColor="#abadc2" title="Links Toevoegen" id="hoofdlinkspanel" left="20" top="66">
              <mx:Label x="0" y="5" text="Titel:"/>
              <mx:Label x="0" y="35" text="Url:"/>
              <mx:TextInput x="68" y="33" id="Url"/>
              <mx:CheckBox x="68" y="63" label="Is Sublinks" id="IsSublink"/>
              <mx:Button x="174" y="85" label="Save"/>
              <mx:Button id="listRemove" label="Remove from List" x="10" y="134"/>
              <mx:Button x="166" y="134" label="Xml laden"/>
              <mx:TextInput x="68" y="3" id="LinkDescription"/>
              </mx:Panel>
              <!-- boomstructuur -->
              <mx:Panel x="315" y="10" width="289" height="366" layout="absolute" borderColor="#abadc2" title="Boomstructuur">
              <mx:Tree id="prodTree" width="269" height="100%" dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" showRoot="false" labelField="@name" right="0" top="0"/>
              </mx:Panel>
              </mx:Canvas>
              <mx:Canvas label="whoei" width="100%" height="394" id="whoei2">
              </mx:Canvas>
              </mx:TabNavigator>


              </mx:Application>
              -------------------------------------------
              Thanx to peter ent by the way.. really clear answer.
              Greets, jacob