locked
Listview null reference exception error RRS feed

  • Question

  • Hi,

    I have 2 listviews whereby the second listview (compNameList1) items source is determined by the selected item of the first listview (compNameList). 

    Here's code snippet for the compNameList_SelectionChanged:

    compDetailList = await objService.GetCompInfoAsync();
    
    var software = from list in compDetailList
                   where list.CompName == (compNameList.SelectedItem as ComputerDetails).CompName
                   select list.CompSoftware;
    
    this.compNameList1.ItemsSource = software;

    Here's the problem that I'm facing; When I deselect the first listview, a null reference exception occur at the following line:

    where list.CompName == (compNameList.SelectedItem as ComputerDetails).CompName

    Thus, how can I put away the error when I deselect the first listview?

    Thanks.

    Regards,

    Eka

    Friday, September 20, 2013 5:25 PM

Answers

  • Use this:

    var software = from list in compDetailList
                   where compNameList.SelectedItem != null 
    		&& compNameList.SelectedItem is ComputerDetails 
    		&& list.CompName == (compNameList.SelectedItem as ComputerDetails).CompName
                   select list.CompSoftware;


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    Friday, September 20, 2013 5:44 PM
  • Hi Eka,

    I will mark the answer from Vishal. And I'm trying to answer your further questions.

    Why must the selected item of the list be declared as not equal to null  -> If compNameList.SelectedItem is null, then compNameList.SelectedItem as ComputerDetails will be NULL as ComputerDetails. It's a kind of protection.

    and is ComputerDetails? -> Let's say if compNameList.SelectedItem is not a ComputerDetails, the as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 23, 2013 1:52 AM
    Moderator

All replies

  • Use this:

    var software = from list in compDetailList
                   where compNameList.SelectedItem != null 
    		&& compNameList.SelectedItem is ComputerDetails 
    		&& list.CompName == (compNameList.SelectedItem as ComputerDetails).CompName
                   select list.CompSoftware;


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!

    Friday, September 20, 2013 5:44 PM
  • Hi Vishal,

    Thanks for your reply. The code you provide works great!

    Could you explain each of the lines so that I can understand better?

    Why must the selected item of the list be declared as not equal to null and is ComputerDetails?

    Thanks.

    Regards,

    Eka

    Saturday, September 21, 2013 11:39 AM
  • Hi Eka,

    I will mark the answer from Vishal. And I'm trying to answer your further questions.

    Why must the selected item of the list be declared as not equal to null  -> If compNameList.SelectedItem is null, then compNameList.SelectedItem as ComputerDetails will be NULL as ComputerDetails. It's a kind of protection.

    and is ComputerDetails? -> Let's say if compNameList.SelectedItem is not a ComputerDetails, the as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 23, 2013 1:52 AM
    Moderator