0
votes

Why cannot access a property or method of a null object reference.

TypeError: Error #1009: Cannot access a property or method of a null object reference. at barm/btn1_clickHandler()[C:\Users\Android\Adobe Flash Builder 4.6\barm\src\barm.mxml:32] at 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 is POS.How to POS is not null.

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

2 Answers

0
votes

The error occurs because you are trying to do something with an object that is null.

The error message says the problem is on line 32. Using the code you have posted, that equates to a line that reads: {

But I am guessing the next line after that probably has the null object:

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

I'm guessing savePosition[index] is null.

You can set a break point here and confirm that in the debugger, trace the value, check for null explicitly, etc.

0
votes

May be the server is not returning any results for the query and hence the line

POS= GetStrDataResult.lastResult; 

is having no effect. So when you basically say POS.toString() it throws the null pointer exception!