请帮助我。无法访问空对象引用的属性或方法

时间:2012-06-01 07:19:37

标签: actionscript-3 flex flex4.5

为什么无法访问null对象引用的属性或方法。

  

TypeError:错误#1009:无法访问空对象引用的属性或方法。       at barm / btn1_clickHandler()[C:\ Users \ Android \ Adob​​e Flash Builder 4.6 \ barm \ src \ barm.mxml:32]       at barm / __ btn1_click()[C:\ Users \ Android \ Adob​​e 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.How到POS不为空。

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

2 个答案:

答案 0 :(得分:0)

发生错误是因为您尝试使用null的对象执行某些操作。

错误消息显示问题在第32行。使用您发布的代码,相当于一行:{

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

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

我猜savePosition[index]为空。

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

答案 1 :(得分:0)

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

POS= GetStrDataResult.lastResult; 

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