Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a small application that allows users to view different types of weeds, flowers and vegetables, I have 3 class Weeds, Flowers and Vegetables.
I would like to clear the list_collection Weed Collection and replace with the Vegetables collection once the GetVegetablesCollection method has been called.
Weeds[] cWeeds = new Weeds[3];
Vegatables[] cVegatables = new Vegatables[2];

private void GetWeedCollection()
{
    cWeeds[0] = new Weeds("Chicory", 100, 47);
    cWeeds[1] = new Weeds("Chinese Cabbage", 105, 37);
    cWeeds[2] = new Weeds("Cress", 81, 76);

    foreach (Weeds x in cWeeds)

    {list_collection.Items.Add(x.Name.ToString());}
}
private void GetVegatablesCollection()
{
    cVegatables[0] = new Vegatables("Cucumber", 5, 3);
    cVegatables[1] = new Vegatables("Carrot", 10, 15);

    foreach (Vegatables x in cVegatables)

    { list_collection.Items.Add(x.Name.ToString()); }


The problem I get is when I call the method, I receive an 'Object reference not set to an instance of an object' on the SelectedItem property.



private void list_collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        if (list_collection.SelectedItem.ToString() == cWeeds[0].Name)
    {
        txt_name.Text = cWeeds[0].Name.ToString();
        txt_cal.Text = cWeeds[0].Calcium.ToString() + "mg";
        txt_phos.Text = cWeeds[0].Phosphrous.ToString() + "mg";
    }

    if (list_collection.SelectedItem.ToString() == cVegatables[0].Name)
    {
        txt_name.Text = cVegatables[0].Name.ToString();
        txt_cal.Text = cVegatables[0].Calcium.ToString() + "mg";
        txt_phos.Text = cVegatables[0].Phosphrous.ToString() + "mg";
    }


Here is the event that calls the method;

private void btn_VegatablesChoice_Click(object sender, RoutedEventArgs e)

        { list_collection.Items.Clear(); GetVegatablesCollection(); }


Hope this made sense, thank you.
Posted

1 solution

Whenever you handle the SelectionChanged event, you have to check if there are any selected items - because if there aren't then you will get a null reference exception when you try to use the SelectedItem property. And you get the event when you clear the list!
Just check at the top of your handler:
C#
private void list_collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    if (list_collection.SelectedItem != null)
        {
        if (list_collection.SelectedItem.ToString() == cWeeds[0].Name)
            {
            txt_name.Text = cWeeds[0].Name.ToString();
            txt_cal.Text = cWeeds[0].Calcium.ToString() + "mg";
            txt_phos.Text = cWeeds[0].Phosphrous.ToString() + "mg";
            }

        if (list_collection.SelectedItem.ToString() == cVegatables[0].Name)
            {
            txt_name.Text = cVegatables[0].Name.ToString();
            txt_cal.Text = cVegatables[0].Calcium.ToString() + "mg";
            txt_phos.Text = cVegatables[0].Phosphrous.ToString() + "mg";
            }
        }
    }
 
Share this answer
 
Comments
SteveBaldwin21 28-Jun-13 14:39pm    
Thank you! That worked perfectly.
OriginalGriff 28-Jun-13 14:54pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900