C# 将列表分配给列表框时,未将对象引用设置为对象异常的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10695611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object reference not set to an instance of an object exception when assigning a list to a listbox
提问by JoBaxter
I wrote the following method to load a list box with values that have not been loaded already but I am getting an Object reference not set to an instance of an object exception when assigning the following. Any information would be helpful. Thanks.
我编写了以下方法来加载具有尚未加载的值的列表框,但是在分配以下内容时,我得到了一个未设置为对象异常实例的对象引用。任何信息都有帮助。谢谢。
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());
}
}
回答by Spencer Ruport
Strings are nullable so at some point you must be doing something similar to:
字符串可以为空,所以在某些时候你必须做类似的事情:
cabinetsCurrentNotUsed.Add(null);
Either that or as dbaseman said it is possible lbxCabinetNameis null but I'm betting that isn't the case.
无论是那个还是 dbaseman 说它可能lbxCabinetName为空,但我敢打赌事实并非如此。
As an aside, it's not really a problem but if you're using a generic list of strings the ToString()call isn't necessary. You can just do this:
顺便说一句,这不是真正的问题,但如果您使用的是通用字符串列表,ToString()则不需要调用。你可以这样做:
lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i]);
回答by Prashanth Thurairatnam
You are trying to add a null to the listbox.
您正在尝试向列表框添加空值。
Insted of
插入的
for (int i = 0; i < cabinetsCurrentNotUsed.Count; i++)
{
lbxCabinetName.Items.Add(cabinetsCurrentNotUsed[i].ToString());
}
use
用
foreach (string s in cabinetsCurrentNotUsed)
{
if(s != null)
lbxCabinetName.Items.Add(s);
}
NOTE
笔记
This part is not relavant to the question. But in your inner for loop after setting cabinetExists = true;you can break out of the inner loop (if atleast one condition is met you can make sure the cabinetExists is true. you don't have to check for the rest of the items in the inner loop)
这部分与问题无关。但是在设置后的内部 for 循环中,cabinetExists = true;您可以跳出内部循环(如果至少满足一个条件,您可以确保cabinetExists 为真。您不必检查内部循环中的其余项目)
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);
}
}
Also if your listBox can be null, make sure you check that first before populating the listbox.
此外,如果您的列表框可以为空,请确保在填充列表框之前先进行检查。
if(lbxCabinetName != null)
{
selectCabinetToAdd();
}
EDIT 2
编辑 2
Dynamically adding control
动态添加控件
ListBox lbxCabinetName = new ListBox();
lbxCabinetName.Location = new System.Drawing.Point(10, 55);
lbxCabinetName.Size = new System.Drawing.Size(130, 95);
this.Controls.Add(lbxCabinetName);

