我正在尝试遍历一个字符串数组,如果它包含某个字符串,则将 1 添加到一个整数,但我在 if 语句中收到错误消息:
NUllReferenceException is unhandled
Object reference not set to an instance of an object
/
for (int i = 0; i < GlobalData.CellH.Length; i++)
{
if (GlobalData.CellH[i].Contains("M"))
{
GlobalData.TotalBoys++;
}
else if (GlobalData.CellH[i].Contains("F"))
{
GlobalData.TotalGirls++;
}
else if (GlobalData.CellH[i].Contains(null))
{
}
}
固定的:
for (int i = 0; i < GlobalData.CellH.Length; i++)
{
if (GlobalData.CellH[i] == "M")
{
GlobalData.TotalBoys++;
}
else if (GlobalData.CellH[i] == "F")
{
GlobalData.TotalGirls++;
}
else if (GlobalData.CellH[i] == "")
{
}
}