2011-01-06 48 views
1

如果我在FlashBuilder执行下面的代码,我得到以下错误(我把它翻译)的Flex/ActionScript3的 - 对象属性/变空

TypeError: Error #1009: Access to an Attribute or Method of an null-Object is not possible. 
at components::NumDisplay()[\src\components\NumDisplay.mxml:39] 

这条线NumDisplay.mxml的问题是:

[Bindable] 
public var oneled_top:OneDisplay = new OneDisplay(numberData.led_top); 

如果我改变它从上面到:

[Bindable] 
public var oneled_top:OneDisplay = new OneDisplay(1); 

它正在工作,因为我发送了一个真实的号码。 那么如何访问numberData.led_top的值?

如果我测试用线

<s:Label text="{numberData.led_top}" color="#FF0000"> 
</s:Label> 

它访问值在samefile NumDisplay.mxml的访问,如果我把它放在我的组件

<components:oneLedDisplay showData="{numberData.led_top}" x="10" y="10" /> 

我一样搜索几个小时后不要得到它... 在此先感谢。

我的主要方法tasachenrechner.mxml

<?xml version="1.0" encoding="utf-8"?> 
<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="600" minHeight="500" xmlns:components="components.*"> 
<fx:Script> 
      <![CDATA[ 
    import components.NumberDisplay; 
    [Bindable] 
    protected var firstNumber:NumberDisplay = new NumberDisplay(1); 
    [Bindable] 
    protected var secondNumber:NumberDisplay = new NumberDisplay(2); 
      ]]> 
</fx:Script> 

<components:NumDisplay 
    numberData="{firstNumber}" 
    x="10" 
    y="20"/> 

<components:NumDisplay 
    numberData="{secondNumber}" 
    x="73" 
    y="20"/> 

</s:Application> 

我的AS-类NumberDisplay.as

package components 
{ 
import flash.display.DisplayObject; 

[Bindable] 
public class NumberDisplay 
{ 
    public var num:Number; 

    public var led_top:Number=0; 
    public var led_r1:Number=0; 
    public var led_r2:Number=0; 
    public var led_middle:Number=0; 
    public var led_l1:Number=0; 
    public var led_l2:Number=0; 
    public var led_bottom:Number=0; 

    public function NumberDisplay(num:Number) 
    { 
    this.num = num; 
    switch(this.num) 
    { 
    case 0: 
    trace("ZERo"); 
    break; 
    case 1: 
    led_top = 1; 
    led_r1 = 1; 
    led_r2 = 1 
    trace("EINS" + led_top + " num:" + num); 
    break; 
          //[... some more cases] 
    default: 
    break; 
    } 
    } 
} 
} 

NumDisplay.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" width="45" height="59" 
    xmlns:components="components.*"> 

<fx:Style> 
    @namespace s "library://ns.adobe.com/flex/spark"; 
    @namespace mx "library://ns.adobe.com/flex/mx"; 
    @namespace components "components.*"; 
</fx:Style> 

<fx:Script> 
    <![CDATA[ 
    import components.NumberDisplay; 
    import components.OneDisplay; 

    [Bindable] 
    public var numberData:NumberDisplay; 

    [Bindable] 
    public var oneled_top:OneDisplay = new OneDisplay(numberData.led_top); 
         // some more init calls of data-objects same type 
    ]]> 
</fx:Script> 


<s:Label text="{numberData.led_top}" color="#FF0000"> 
</s:Label> 

<components:oneLedDisplay showData="{oneled_top}" x="10" y="10" /> 
     // some more objects of same type 
</s:Group> 

AS级OneDisplay.as

package components 
{ 
import flash.display.DisplayObject; 

public class OneDisplay 
{ 
    [Bindable] 
    public var show:Number; 
    [Bindable] 
    public var value:Number=0; 

    public function OneDisplay(show:Number) 
    { 
    this.show = show; 
    switch(this.show) 
    { 
    case 0: 
    value = 0.3; 
    trace(value); 
    break; 
    case 1: 
    value = 1.0; 
    trace(value); 
    break; 
    } 
    } 
} 
} 

oneLedDisplay.mxml

<?xml version="1.0" encoding="utf-8"?> 
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"> 
<fx:Script> 
    <![CDATA[ 
    import components.OneDisplay; 
    [Bindable] 
    public var showData:OneDisplay; 
    ]]> 
</fx:Script> 

<s:Rect id="stroke" width="40" height="6" alpha="{showData.value}"> 
    <s:fill> 
    <s:SolidColor color="#000000"/> 
    </s:fill> 
</s:Rect> 

<s:Label text="{showData.value}" color="#FF0000"> 
</s:Label> 

</s:Group> 

回答

4

记住,你不仅是分配一个值,但声明成员变量oneled_top。在这一点上,你不能访问numberData,因为它没有被实例化(没有呼叫new NumberData()!你必须找到一种方法,让你在稍后访问new OneDisplay (numberData.led_top)时,实际上有一个访问值。

+0

+1对这个问题的解释。就未来有访问价值的时间而言,我建议使用commitProperties()(正如我在答案中所做的那样)。 – JeffryHouser 2011-01-06 14:44:47

2

您提供大量的代码,我不想进行逆向工程。

答案是oneled_top正在numberData之前进行初始化。使用MXML时,您有过的变量初始化没有控制权。

设置默认commitProperties()方法中的值,或者如果oneled_Top应该是外观部件,请在PartAdded方法中设置默认值。

您将受益于Component Lifecycle的阅读。

0

您可以使用BindingUtils.bindSetter()检测更改numberData然后初始化oneled_top

BindingUtils.bindSetter(_setOneLabel_top, this, "numberData"); 

和setter:

function _setOneLabel_top(disp:NumberDisplay):void 
{ 
    /* if(this.oneled_top == null) */ 
    this.oneled_top = new OneDisplay(disp.led_top); 
} 

但我认为,你就像使用[Bindable],你不应该必须需要它。