I hit this problem quite often and wonder if there is a general
solution...

I have a component which displays a list of questions and has the
concept of a "current" question i.e. the question the user is answering:

        <mx:Script>
                <![CDATA[
                        import mx.collections.ArrayCollection;
                        
                        [Bindable]                      
                        public var questions:ArrayCollection;
                                                
                        [Bindable]
                        public var currentQuestion:MultipleChoiceQuestion =
questions[lstBox.selectedIndex];
                ]]>
        </mx:Script>

The problem is that the questions array doesn't get populated until
after Flex has tried to setup all the bindings meaning I get a
NullPointer when it tries to setup "currentQuestion".

At the moment I have to get around this with the following code:

        <!-- Databinding of Current Question -->
        <mx:Script>
                <![CDATA[
                        private function init():void {
                                refreshCurrentQuestion();
                        }
                        
                        public function refreshCurrentQuestion():void {
                                if (questions != null) {
                                        currentQuestion = 
questions[lst.selectedIndex];
                                }                       
                        }
                ]]>
        </mx:Script>

This seems quite hacky...

Reply via email to