Removing an item from an ObservableCollection causes crash, why?

0

I declared an ObservableCollection<T> of string, like so:

private ObservableCollection<string> riStrips = new ObservableCollection<string>();

public ObservableCollection<string> RiStrips
{
    get
    {
        return riStrips;
    }
    set
    {
        riStrips = value;
        OnPropertyChanged("RiStrips");
    }
}

I have a function that adds to the collection, called by a button click passing a string to it:

public void AddRiStrip(string riStrip)
{
    if (riStrips.Contains(riStrip))
    {
        MessageBox.Show("Duplicate strip already in list.");
        return; //no dupes
    }
    riStrips.Add(riStrip);
    OnPropertyChanged("RiStrips");
}

and that works. I have a function that removes an item also called by a button click with a string being passed:

public void RemoveStrip(string riStrip)
{
    if (riStrips.Contains(riStrip)) riStrips.Remove(riStrip);//crashes on this line
    OnPropertyChanged("RiStrips");
}

and that causes the program to crash with this exception:

    System.NullReferenceException
    HResult=0x80004003
    Message=Object reference not set to an instance of an object.
    Source=Message Template

This function is in a secondary window. I have an identical (except for variable names) function in the main window and it works. What have I missed?

c#
wpf
asked on Stack Overflow Sep 22, 2019 by Buck • edited Sep 22, 2019 by Peter Duniho

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0