-1

I want to add the items of a listBox to a list inside Properties.Settings.Default but it doesn't work at all. I add items to my listBox with a messagebox i added from the Microsoft.VisualBasic library and then add the input to the listBox:

private void Button_additem_Click(object sender, EventArgs e)
{
    string itemname = Interaction.InputBox("Item hinzufügen", "Gib unten den Namen des Items ein.", "");
    if (itemname.Length > 0)
    {
        listbox_items.Items.Add(itemname);
    }
    else { }
    Save();
    Restore();
}

After that i am using my Save() and Restore() voids:

private void Save()
{
    if (Properties.Settings.Default.myList == null)
    { }
    else 
    { 
        Properties.Settings.Default.myList.Clear(); 
    }

    foreach (object item in listbox_items.Items)
    {
        Properties.Settings.Default.myList.Add(item);
    }

    Properties.Settings.Default.Save();
}

private void Restore()
{
    combobox_montag.Items.Clear();
    combobox_dienstag.Items.Clear();
    combobox_mittwoch.Items.Clear();
    combobox_donnerstag.Items.Clear();
    combobox_freitag.Items.Clear();
    combobox_samstag.Items.Clear();
    combobox_sonntag.Items.Clear();
    listbox_items.Items.Clear();

    foreach (object item in Properties.Settings.Default.myList)
    {
        combobox_montag.Items.Add(item);
        combobox_dienstag.Items.Add(item);
        combobox_mittwoch.Items.Add(item);
        combobox_donnerstag.Items.Add(item);
        combobox_freitag.Items.Add(item);
        combobox_samstag.Items.Add(item);
        combobox_sonntag.Items.Add(item);
        listbox_items.Items.Add(item);
    }
}

The error comes here with a 'System.NullReferenceException' 'Planer.Properties.Settings.myList.get returned null.':

foreach (object item in listbox_items.Items)
{
    Properties.Settings.Default.myList.Add(item);  <---------
}
Joelius
  • 3,839
  • 1
  • 16
  • 36
R00T
  • 7
  • 1
  • Your if statement in the second code block doesn't really make sense. You should do `if (myList != null) ...` instead of an empty if and the code in the else. Also you could do `myList?.Clear()` which will only execute the methode if `myList` isn't null. It comes down to preference if you want to use that operator though. Furthermore in the first code block there's an empty else which can be completely removed. – Joelius Jun 29 '19 at 13:19
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Joelius Jun 29 '19 at 13:30

1 Answers1

0

There is a part of code that can make this list null

        if (Properties.Settings.Default.myList == null)
        { } 
        else { Properties.Settings.Default.myList.Clear(); 

this if makes nothing, if the list is null it can not add an item, null is not the same as empty null means inexsintent, you will need to create a new one seeing as you only cear the list in the else so:

if (Properties.Settings.Default.myList == null)
        {Properties.Settings.Default.myList = new List<?> etc... }