Apache flex flex/actionscript分配失败?
我在我的动作脚本代码中看到了一些奇怪的东西 我有两门课:foo和bar,bar和foo。在一个模型类中,我有一个foo成员变量,我为foo变量分配了一个bar对象。但是赋值之后,foo变量为nullApache flex flex/actionscript分配失败?,apache-flex,actionscript-3,actionscript,null,flexbuilder,Apache Flex,Actionscript 3,Actionscript,Null,Flexbuilder,我在我的动作脚本代码中看到了一些奇怪的东西 我有两门课:foo和bar,bar和foo。在一个模型类中,我有一个foo成员变量,我为foo变量分配了一个bar对象。但是赋值之后,foo变量为null [Bindable] public var f:foo; public function someFunc(arr:ArrayCollection):void { if(arr.length > 0) { var tempBar:bar = arr.getItemAt(0)
[Bindable] public var f:foo;
public function someFunc(arr:ArrayCollection):void {
if(arr.length > 0) {
var tempBar:bar = arr.getItemAt(0) as bar;
if(tempBar != null) {
tempBar.someProp++;
f = tempBar;
// f is now null
}
}
}
你知道我做错了什么吗
编辑
以下是确切的代码:
[Bindable] public var selectedCustomerJob:IDSCustomer;
public function selectedJobByIdCallback(evt:Event):void
{
var temp:IDSDTOArrayCollection = evt.currentTarget as IDSDTOArrayCollection;
if(null != temp && temp.length > 0)
{
selectedCustomerJob = IDSJob(temp.getItemAt(0));;
trace(" selectedCustomerJob: " + flash.utils.getQualifiedClassName(selectedCustomerJob));
trace(" jobToSelect type: " + flash.utils.getQualifiedClassName(temp.getItemAt(0)));
trace("jobToSelect super class: " + flash.utils.getQualifiedSuperclassName(temp.getItemAt(0)));
}
}
这是跟踪输出:
selectedCustomerJob:空
作业选择类型:com.intuit.sb.cdm.v2::IDSJob
jobToSelect super class:com.intuit.sb.cdm.v2::IDSCustomer使用
as
关键字强制转换失败时返回null
。在这种情况下,数组集合中的第一项可能不是您期望的Bar
类型的对象;它可能是一个Foo
或其他东西。您可以将子类对象强制转换为基类,但不能以其他方式
使用括号语法进行强制转换-如果强制转换失败,它将引发异常,因此您可以确定arr.getItemAt(0)
的类型
确保数组集合中的第一项确实是Bar
实例(而不是Foo
或其他内容)
否则,您可以使用
trace(flash.utils.getQualifiedClassName(arr.getItemAt(0)));
顺便说一句,我相信发布的代码与您运行的代码不完全相同,因为要使
f
为null
,tempBar
在分配给f
时应该为null
。在这种情况下,在检查if
中的null
时,不应执行if
中的代码。即使它进入if
块,它也会在您试图增加tempBar.someProp的第一行抛出一个空指针错误(#1009)
trace(flash.utils.getQualifiedClassName(arr.getItemAt(0)));
if(tempBar != null) {
tempBar.someProp++;
f = tempBar;
// f is now null
}