I am very new to actionscript3/flashbuilder and I’m trying to do something simple like pass a value from one view to another view.
I worked out all my syntax errors, but I am still getting a NULL error
(TypeError: Error #1009: Cannot access a property or method of a null object reference)
…even though I have the values hard-coded in for testing.
I have 2 views, and when I press a button, it is supposed to pass a value to another view which then displays the passed value in a label.
Here are my 2 views:
view1:
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="view1">
<fx:Script>
<![CDATA[
var value1:int = 1;
var value2:int = 2;
]]>
</fx:Script>
<s:Button id="btn1" x="5" y="10" width="25" label="Button 1" click="navigator.pushView(view2, {val:value1})"/>
<s:Button id="btn2" x="15" y="100" width="25" label="Button 2" click="navigator.pushView(view2, {val:value2})"/>
</s:View>
view2:
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="view2">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
var passedValue:int = data.val;
]]>
</fx:Script>
<s:Label x="75" y="100" width="200" text="{passedValue}" />
Any help would be appreciated!
Thanks!
Your problem is you’re trying to access the data object before the view is done being created.
Add a creationComplete handler to your view:
And inside your init method you will be able to access the data object
Also, notice I added [Bindable] to your passedValue var.
This is because you’re tying to bind its value to your label. In order for that to work you need to use [Bindable] on your var