Apache flex 如何摆脱空StyleProtoChain对象引用?

Apache flex 如何摆脱空StyleProtoChain对象引用?,apache-flex,actionscript-3,Apache Flex,Actionscript 3,以下是错误: 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\StyleProtoChai

以下是错误: 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] 也许你可以教我如何在复杂类中保留空对象引用

塔克斯


Dave

看起来错误可能来自对MessageBoard类构造函数中addChild()方法的调用。您很可能在UIComponent生命周期的早期调用此方法。我建议重写createChildren()方法并在其中添加子元素


你能演示一下你在说什么吗?我想我现在理解了如何创建复合对象,多亏了你的轻推:“this.addChild”部分必须推到createChildren方法中。如果有其他的.addChild部分,它们必须进入自己的类,在这些类中重复该过程。在大多数情况下,这是正确的。有时,您需要创建子级,作为在自定义组件上设置属性的结果。在这种情况下,为属性编写一个setter,并在其中调用invalidateProperties();这将导致稍后调用commitProperties()。然后可以重写commitProperties(),并根据需要动态创建或销毁子级。这与许多内置Flex组件使用的机制相同,了解其工作原理非常有用。:)
<?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>
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
override protected function createChildren():void
{
    super.createChildren();
    _messageTitle = new Label();
    _messageTitle.text = "Message Board";
    this.addChildAt(_messageTitle, 0);
}