CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2001
    Location
    Switzerland
    Posts
    114

    SelectedIndexChanged (ListView) throws always unexpected exception

    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
    Read Ouspensky 's "Tertium Organum"!

  2. #2
    Join Date
    Nov 2002
    Location
    Tatooine
    Posts
    155
    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.
    That which does not kill us, only makes us stronger.

    MCSD .NET

  3. #3
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890

    Thumbs up

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured