Skip to main content
March 5, 2008
Question

Accessing selected Item of a lIst

  • March 5, 2008
  • 3 replies
  • 345 views
Hi,

I'm trying to access the selected item idex label of a click list.

I normally havn' t had any trouble with this before. I seem to be having a problem because the list is inside a movie clip.

First of all I attach a movieclip containing a list to a container

///////////////////////////////////////////////////////////////////////

var player:Object = getDefinitionByName("Player") as Class;
var clipPlayer:MovieClip = new player();
clipPlayer.name="player"
clipPlayer.x=200;
clipPlayer.y=200;
Container.addChild(clipPlayer);

//////////////////////////////////////////////////////////////////////////

Then I add data to the list

///////////////////////////

var temp:Array=arr;
for (var i=0; i<temp.length; i++) {

var obj:Object=temp ;
var id:int =obj.ID;
var d:String=obj.Name;
var s:String=obj.SourcePath;
//trace("test"+d+"***")

var c=Container.getChildAt(0);
c.clipListBox.addItem({label:d,data:s});
c.clipListBox.addEventListener(ListEvent.ITEM_CLICK,clipItemClicked);
}

/////////////////////////
In the last line of the above code I have added an event listener named clipItemClicked.

The problem I have is in the handler for this listener.
////////////////////////////////////

public function clipItemClicked(e:ListEvent):void
trace(e.target.selectedItem.label);
}

///////////////////

When I try the above code (which I think is fairly standard) I get the following error and I dont understand why?

TypeError: Error #1009: Cannot access a property or method of a null object reference.

As far as I can see the property is not null because the list is displaying data. I'm not sure what the whole null object reference thing means.

Any ideas why I'm getting this error?

Thanks
dub
This topic has been closed for replies.

3 replies

March 5, 2008
Thanks alot!
Inspiring
March 5, 2008
You're probably getting the error on the first click only. That would be
because the ITEM_CLICK event is sent before the selectedItem is changed
(initially null).

If all you want to do is act on an item change, you should listen for
Event.CHANGE.

BTW, you need to add the event listener only once, so take it out of the
loop.


March 5, 2008
Ok this is really weird.

The first time I click on the list box e.selectedItemIndex.label is equal to null. Thats why I get the error.

Any time after that that I click the values trace out normally?

Very confusing. Can someone help?
March 5, 2008
Sorry is this becomes a duplicate. Posted 90 minutes ago using nntp. I guess something ain't a-working this morning.

The ITEM_CLICK event is sent before the selectedItem is changed
(initially null).

If all you want to do is act on an item change, you should listen for
Event.CHANGE.

BTW, you need to add the event listener only once, so take it out of the
loop.