0

为什么不能访问空对象引用的属性或方法。

TypeError:错误 #1009:无法访问空对象引用的属性或方法。在 barm/btn1_clickHandler()[C:\Users\Android\Adobe Flash Builder 4.6\barm\src\barm.mxml:32] 在 barm/__btn1_click()[C:\Users\Android\Adobe Flash Builder 4.6\barm\ src\barm.mxml:65]

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:service="services.service.*"
                   >
<fx:Script>
    <![CDATA[
        import com.adobe.serializers.utility.TypeUtility;
        import flash.sampler.DeleteObjectSample;    
        import flashx.textLayout.operations.DeleteTextOperation;
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        import spark.events.IndexChangeEvent;
        [Bindable]
        private var positionChoose:ArrayCollection = new ArrayCollection();
        protected function btn1_clickHandler(event:MouseEvent):void
        {
            // TODO Auto-generated method stub
            GetStrDataResult.token = service.GetStrData(
    "SELECT PositionCode,LocalDesc FROM StaffPosition 
    WHERE LocalDesc IN ('พนักงานขาย','Administrator','Trainner')","StaffPosition");
        var POS:Object ;            
        POS= GetStrDataResult.lastResult; 
    var savePosition:Array = new Array(POS.toString().split('|').length);
    var savePositionID:Array = new Array(POS.toString().split('|').length);
    var savePositionName:Array = new Array(POS.toString().split('|').length);
    var index:int;
    savePosition = POS.toString().split('|');
    for( index = 0; index < savePositionID.length; index++ )
            {
                savePositionID[index] = savePosition[index].split('^')[0];
            }

    for( index = 0; index < savePositionName.length; index++ )
            {
    savePositionName[index] = savePosition[index].split('^')[1];
    positionChoose.addItem(savePositionName[index]); 
            }
        }   
    ]]>
</fx:Script>
<fx:Declarations>
    <s:CallResponder id="GetStrDataResult"/>
    <service:Service id="service"
    fault="Alert.show(event.fault.faultString + '\n'+event.fault.faultDetail)"
                     showBusyCursor="true"/>

    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:DropDownList id="dropDownList" x="39" y="31" width="164"
                dataProvider="{positionChoose}"></s:DropDownList>
<s:Button id="btn1" x="132" y="64" label="Button"    
              click="btn1_clickHandler(event)"/>
</s:WindowedApplication>

null 是 POS。如何 POS 不为 null。

var POS:Object;         
POS = GetStrDataResult.lastResult; 
var savePosition:Array = new Array(POS.toString().split('|').length);
4

2 回答 2

0

发生错误是因为您试图对null.

错误消息说问题出在第 32 行。使用您发布的代码,这相当于一行内容:{

但我猜测之后的下一行可能有空对象:

savePositionID[index] = savePosition[index].split('^')[0];

我猜savePosition[index]是空的。

您可以在此处设置断点并在调试器中确认、跟踪值、显式检查 null 等。

于 2012-06-01T07:36:04.263 回答
0

可能是服务器没有返回任何查询结果,因此该行

POS= GetStrDataResult.lastResult; 

没有效果。所以当你基本上说它POS.toString()抛出空指针异常!

于 2012-06-01T12:45:13.440 回答