如何摆脱null StyleProtoChain对象引用?

时间:2009-05-29 21:32:03

标签: 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\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

也许你可以告诉我如何保持空对象引用不受复杂类的影响?

感谢名单

戴夫

1 个答案:

答案 0 :(得分:2)

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

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