如果我在FlashBuilder中执行下面的代码,我会得到以下错误(我将其翻译)
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?访问该值呢?
如果我用行测试samefile NumDisplay.mxml中的访问
<s:Label text="{numberData.led_top}" color="#FF0000">
</s:Label>它访问该值,就像我将其放入组件中一样。
<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>发布于 2011-01-06 14:39:21
请记住,您不仅分配了一个值,还声明了成员变量oneled_top。此时,您无法访问numberData,因为它还没有实例化(没有调用new NumberData()!您必须找到一种方法,在以后的某个时候调用new OneDisplay (numberData.led_top),而实际上有一个要访问的值。
发布于 2011-01-06 14:43:39
你提供了很多代码,我不想逆向工程。
答案是oneled_top是在numberData之前初始化的。在使用MXML时,您无法控制变量的初始化。
在commitProperties()方法中设置默认值,或者如果oneled_Top应该是皮肤部分,则在PartAdded方法中设置默认值。
阅读组件生命周期将使您受益。
发布于 2011-02-23 09:21:13
您可以使用BindingUtils.bindSetter()检测numberData的更改,然后初始化oneled_top。
BindingUtils.bindSetter(_setOneLabel_top, this, "numberData");和策划人:
function _setOneLabel_top(disp:NumberDisplay):void
{
/* if(this.oneled_top == null) */
this.oneled_top = new OneDisplay(disp.led_top);
}但是我认为,你使用的是[Bindable],你不应该需要它。
https://stackoverflow.com/questions/4615847
复制相似问题