1

I am creating an interface that has a few different states for the different steps. For those steps, there is data that I am pulling in from a database to fill certain fields.

As of right now I am doing one db query to get all of the data back and want to fill in all of the fields at the same time but it is giving me "access to a null object reference".

It seems as though there is a scope issue when you are trying to access a text input field with actionscript when the state that the text input is in, isn't the current state.

Is there any way around this?

For Example (This would throw a "Null object reference" error):

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           creationComplete="init()">
<s:states>
<s:State name="State1"/>
<s:State name="state2"/>
</s:states>
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        protected function init(event:FlexEvent):void
        {
            ti_test.text = "Hello World";
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextInput id="ti_test" includeIn="state2" x="323" y="197"/>
</s:Application>
Kevin Mann
  • 11
  • 3

2 Answers2

3

Flex creates the states only when they are needed (not even right after you change the state they are not available because flex waits for the next render cycle to make them, thus optimizing the process). This can be harder to work with, however, sometimes, some objects are needed before or right after you change the state, for those objects use itemCreationPolicy=immediate this removes the flex optimization but allows you to use the object right away.

So, to answer your question, adding itemCreationPolicy=immediate to your text field should solve your problem without any additional work.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
0

Flex will only make the current state. So you can only call the init() function when the textinput is created. You can do that like this :

<s:TextInput id="ti_test" includeIn="state2" creationComplete"init()" x="323" y="197"/>

Now you say you want to put the value there even before it is created, I am afraid that is impossible. What you need to do is make an actionscript class that represents your database object. Then assign the proper values to an instance of that class. And then you put like this

<s:TextInput id="ti_test" includeIn="state2" text="{dataObject.textValue}" x="323" y="197"/>

If you really don't want to make a class, then you can just put all different variables in your application and then assign the data to that. You would then have something like this

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           creationComplete="init()">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        [Bindable]
        var textValue:String;

        protected function init(event:FlexEvent):void
        {
            textValue = "Hello World";
        }

    ]]>
</fx:Script>
<fx:Declarations>

</fx:Declarations>

<s:states>
<s:State name="State1"/>
<s:State name="state2"/>
</s:states>
<s:TextInput id="ti_test" text="textValue" includeIn="state2" x="323" y="197"/>
</s:Application>
Sparky
  • 717
  • 1
  • 7
  • 17
  • Yours works, yes, but I am trying to figure out how I would assign a value to the ti_test with actionscript while I am still on state 1. – Kevin Mann May 30 '11 at 00:01
  • The data that I am getting is coming from a database. After I do the database query, the result function is called. When that is called, I would like to fill in all of the information for each state instead of storing it and filling it in when they get to the state. – Kevin Mann May 30 '11 at 00:09
  • @Kevin Mann : You will have to store it, in the main application or in a separate class, that's up to you. There is no other way ! – Sparky May 30 '11 at 00:29
  • 1
    Kevin, the only way to get access to the TextInput in the other states is to remove the `includeIn="state2"` since that actually removes the instance of that control. If anything, use `visible="false" visible.state2="true"`. – J_A_X May 30 '11 at 00:59
  • Agree with the first part of this answer, but you can force the immediate creation of the object, thus, it is possible without the creation complete handler – Rad'Val May 30 '11 at 09:20