Click to See Complete Forum and Search --> : SelectedIndexChanged (ListView) throws always unexpected exception
wotan
December 19th, 2002, 12:37 PM
private void list_SelectedIndexChanged(object sender, System.EventArgs e)
{
int itemindex=list.FocusedItem.Tag; (this throws the exception)
...
...
...
...
...
}
NullReferenceException is throwed!
What's wrong with this code?
Thanks
Stefan
wolfofthenorth
December 19th, 2002, 03:03 PM
I had a similar problem and what I noticed was that this event seemed to fire with a null object between actual items.
You could either test to see if the item is null or wrap it in a try block and handle it accordingly.
Hope this helps. :D
pareshgh
December 19th, 2002, 05:05 PM
int itemindex=list.FocusedItem.Tag; (this throws the exception)
the reason is you want the selected item !!!
the Tag is an object. since you can associate any object with Tag property but if you haven't then at the run time it will be null so that it throws exception. again if you do have associated an item object with the Tag of list item then you would cast it to a proper class where it was originally associated. if it still turns out to be null then you have to check for that also.
so some thing like this.
list.Tag = i; // i being integer object
again somewhere in code...
int x = (int) list.Tag; ...
hope this helps
-Paresh
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.