An object reference is not set to an object exception instance when assigning a list to a list

I wrote the following method to load a list with values ​​that have not yet been loaded, but I get a reference to an object that is not installed in the object exception instance when assigning the following. Any information would be helpful. Thank you

lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());

// Defined outside a method
List<string> cabinetsCurrentNotUsed; 

// Set value in the constructor
cabinetsCurrentNotUsed = new List<string>();

Here is the whole procedure.

    private void selectCabinetToAdd()
    {

        // Loop through all server and form cabinet types to see if there are matches
        for (int x = 0; x < opticalFile.serverCabinetNames.Count; x++)
        {
            bool cabinetExists = false;
            for (int i = 0; i < opticalFile.CabinetValues.Count; i++)
            {
                if (opticalFile.serverCabinetNames[x].ToString() == opticalFile.CabinetValues[i].ToString())
                {
                    cabinetExists = true;
                }
            }
            // Add cabinets not used to cabinetsCurrentNotUsed List
            if (!cabinetExists)
            {
                cabinetsCurrentNotUsed.Add(opticalFile.serverCabinetNames[x].ToString());
            }
        }

        // Send cabinetsCurrentNotUsed List to list box
        for (int i = 0; i < cabinetsCurrentNotUsed.Count; i++)
        {
            lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());
        }
    }
+3
source share
2 answers

You are trying to add zero to the list.

Established

for (int i = 0; i < cabinetsCurrentNotUsed.Count; i++)
{
     lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());
}

using

foreach (string s in cabinetsCurrentNotUsed)
{
    if(s != null)
          lbxCabinetName.Items.Add(s);
}

Note

This part is not relevant. But in your inner loop after installation, cabinetExists = true;you can exit the inner loop (if at least one condition is met, you can make sure that true. Inner loop)

EDIT

private void selectCabinetToAdd()
{
        foreach (string sc in serverCabinetNames)
        {
            bool cabinetExists = false;
            foreach (string cv in CabinetValues)
            {
                if (sc == cv)
                {
                    cabinetExists = true;
                    break;
                }                    
            }

            if (!cabinetExists)
            {
                cabinetsCurrentNotUsed.Add(sc);
            }

        }

        foreach (string ccnu in cabinetsCurrentNotUsed)
        {
            if (ccnu != null)
                lbxCabinetName.Items.Add(ccnu);
        }
   }

, listBox , , , .

if(lbxCabinetName != null)
{
    selectCabinetToAdd();
}

2

ListBox lbxCabinetName = new ListBox();
lbxCabinetName.Location = new System.Drawing.Point(10, 55);
lbxCabinetName.Size = new System.Drawing.Size(130, 95);
this.Controls.Add(lbxCabinetName);
+2

NULL, - - :

cabinetsCurrentNotUsed.Add(null);

, dbaseman , lbxCabinetName null, , .

, , ToString() . :

lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i]);
0

All Articles