2009-05-29 67 views
0

以下是錯誤:如何擺脫空StyleProtoChain對象引用?

 
    TypeError: Error #1009: Cannot access a property or method of a null object reference. 
     at mx.styles::StyleProtoChain$/initProtoChainForUIComponentStyleName()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:72] 
     at mx.core::UIComponent/ http://www.adobe.com/2006/flex/mx/internal::initProtoChain()[C :\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:7469] 
     at mx.core::UIComponent/regenerateStyleCache()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:7690] 
     at mx.core::UIComponent/ http://www.adobe.com/2006/flex/mx/internal::addingChild()[C :\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:5239] 
     at mx.core::UIComponent/addChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:4955] 
     at mx.controls.listClasses::ListBase/createChildren()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\controls\listClasses\ListBase.as:3103] 
     at mx.core::UIComponent/initialize()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:5370] 
     at mx.core::UIComponent/ http://www.adobe.com/2006/flex/mx/internal::childAdded()[C :\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:5267] 
     at mx.core::Container/ http://www.adobe.com/2006/flex/mx/internal::childAdded()[C :\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:3305] 
     at mx.core::Container/addChildAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2217] 
     at mx.core::Container/addChild()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Container.as:2140] 
     at model::MessageBoard()[C:\Documents and Settings\dbabbitt\My Documents\Flex Builder 3\ListExample\src\model\MessageBoard.as:56] 
     at model::MessageBoard$cinit() 
     at global$init()[C:\Documents and Settings\dbabbitt\My Documents\Flex Builder 3\ListExample\src\model\MessageBoard.as:7] 
     at main()[C:\Documents and Settings\dbabbitt\My Documents\Flex Builder 3\ListExample\src\main.mxml:56] 
     at _main_mx_managers_SystemManager/create() 
     at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3188] 
     at mx.managers::SystemManager/ http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C :\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3064] 
     at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2916]

這裏是main.mxml:

 
    <?xml version="1.0"?> 
    <mx:Application 
     xmlns:mx="http://www.adobe.com/2006/mxml" 
    > 
     <mx:Script> 
      <![CDATA[ 
       import model.MessageBoard; 
       public var _messageBoard:MessageBoard = MessageBoard.instance; 
      ]]> 
     </mx:Script> 
    </mx:Application> 
這裏是MessageBoard.as:

package model { 
    import mx.containers.VBox; 
    import mx.controls.Label; 

    [Bindable] 
    public class MessageBoard extends VBox { 

     /** The message list title. */ 
     private var _messageTitle:Label; 

     /** The maximum message count. */ 
     private var _maxMessageCount:int = 4; 

     /** Storage for the singleton instance. */ 
     private static const _instance:MessageBoard = new MessageBoard(SingletonLock); 

     /** Provides singleton access to the instance. */ 
     public static function get instance():MessageBoard { 
      return _instance; 
     } 

     /** 
     * Constructor 
     * 
     * @param lock The Singleton lock class to pevent outside instantiation. 
     */ 
     public function MessageBoard(lock:Class) { 
      super(); 

      // Verify that the lock is the correct class reference. 
      if (lock != SingletonLock) { 
       throw new Error("Invalid Singleton access. Use MessageBoard.instance."); 
      } 

      _messageTitle = new Label(); 
      _messageTitle.text = "Message Board"; 
      this.addChild(_messageTitle); 
     } 

    } // end class 
} // end package 

class SingletonLock { 
} // end class 

,也許你可以學我如何保持空的對象引用了複雜的類的?

感謝名單

戴夫

回答

2

它看起來像錯誤可以從電話到您的留言板類的構造函數的addChild()方法到來。您可能很早就在UIComponent生命週期中調用此方法。我會建議覆蓋createChildren()方法並在其中添加您的子元素。

override protected function createChildren():void 
{ 
    super.createChildren(); 
    _messageTitle = new Label(); 
    _messageTitle.text = "Message Board"; 
    this.addChildAt(_messageTitle, 0); 
} 
+0

你能證明你在說什麼嗎? – 2009-05-30 11:18:39