ObjectType Object is null even a value is being passed to it

90 Views Asked by At

I am having trouble with selectIndexChanged of my form.

For example I have country A and country B added to the countryComboBox

I can now select a country with the use of selectIndexChanged of the countryComboBox. Once selected I can add states. Once states are added it will be shown in the countryStateListBox then I can select a state to add town to it. Towns will appear on the townListBox.

The problem I am getting is if I select country B and then come back to the country A, I can view the States in countryStatesListBox but not the towns of each state in townListBox . Although I checked the memory and it was added correctly but cant view it on the townListbox properly.

Basically once Ive returned to the previous country i added stuff with, countryStateListBox_SelectIndexChanged will kick in.

the line in countryStateListBox_SelectIndexChanged

CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

is where I am having problem with.

The bit :

country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

has value on it but it is Not being passed to the object "state". Although it worked before changing to another country. Any ideas how to resolve this?

Ive been stuck for a whole day debugging this. Any Help would be great, ty.

 private void countryComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {           
        Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));

        if (country != null)
        {
            countryStateListBox.Items.Clear();
            townListBox.Items.Clear();
            country.countryStateList.ForEach(x => countryStateListBox.Items.Add(x));
        }
    }




private void countryStateListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));
        CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));// problem here!

        if (state != null)
        {
            townListBox.Items.Clear();
            state.Towns.ForEach(x => townListBox.Items.Add(x));
        }
    }
2

There are 2 best solutions below

7
On

You need to detect if it was the user who triggered the change on the ListBoxes or you programatically

private bool triggeredByUser = true ;

private void countryComboBox_SelectedIndexChanged(object sender, EventArgs e)
{           
    Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));

    if (country != null)
    {
        triggeredByUser = false ;

        countryStateListBox.Items.Clear();
        //townListBox.Items.Clear();
        country.countryStateList.ForEach(x => countryStateListBox.Items.Add(x));

        //by default select the first state
        countryStateListBox.selectedIndex = 0 ;
        CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

        //refresh the town list based on the country and state
        refreshTownList(country,state) ;

        triggeredByUser = true ;
    }
}




private void countryStateListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if(triggeredByUser)
    {
        Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));
        CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

        //refresh the town list based on the country and state
        refreshTownList(country,state) ;
    }
}

private refreshTownList(Country country,CountryState state)
{
    if (state != null)
    {
        townListBox.Items.Clear();
        state.Towns.ForEach(x => townListBox.Items.Add(x));
    }
}
1
On

Your problems with the townListBox not being populated is because in your countryComboBox_SelectedIndexChanged method, you have a line for populating the countryStateListBox but not the townListBox. Add a line for populating the townListBox in the countryComboBox_SelectedIndexChanged method.