I am writing an application, and I want to store a list of files selected by the user. Currently, one of my settings is a StringCollection called filesToFetch, which is User scoped and contains the paths of all the files that the program should fetch. I have a button that allows the user to add new files to the list. This is the code for the button click event
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
Properties.Settings.Default.filesToFetch.Add(openFileDialog1.FileName);
Properties.Settings.Default.Save();
}
}
When I try to add a new file to the StringCollection, I get the error
NullReference Exception was unhandled
Object reference not set to an instance of an object.
I think that this may be happening because filesToFetch has not been initialized, but I'm not really sure. I could be wrong, but I thought that an object gets a name when it is initialized, and since my settings all get names at Design time, I assumed that they are automatically initialized when the program runs, but now I think I might be wrong about this. Is this the issue, or am i missing something else?
Here is a screen capture of my settings for reference.
I should probably explain a bit further. Let's say you were going to use a list of strings. You can declare:
IList<string> a;
At this point a = null and null does not have an Add method. If you initialize:
IList<string> a = new List<string>();
Now a = an empty list of strings. It will at this point have an Add method to use to add strings to the list.