运行我的代码时,我在文本框的不同行上放置了多个字符串,但是它断了,“ Items.Add(item)”上出现Null Exception错误,我不确定为什么我得到了
此错误是因为在Visual Studio中,变量项中的字符串不为null,它包含
一个返回字符,所以我不确定这是否是问题。例如item =“ uno \ r”。另外,Items是字符串列表。有谁知道为什么我不断收到此Null异常?
public List<string> Items;
public void getItemsFromTextBox(TextBox textbox)
{
string[] lines = textbox.Text.Split('\n');
foreach (string item in lines)
{
if (!String.IsNullOrWhiteSpace(item))
Items.Add(item);
}
}
最佳答案
您尚未初始化列表,它为空!加
public List<String> Items = new List<String>();
关于c# - 空异常检查字符串是否为空时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194784/