【发布时间】:2014-05-22 23:59:57
【问题描述】:
我有以下代码示例:
public int intTest { get; set; }
public List<int> listTest { get; set; }
//Method for creating a random number to return.
public int methodRandom()
{
Random random1 = new Random();
int intValue;
intValue = random1.Next(1, intTest + 1);
return (intValue);
}
private void button1_Click(object sender, EventArgs e)
{
intTest = int.Parse(textBox1.Text); // Will be a value between 1 and 9 in my code)
for (int i = 0; i < intTest; i++)
{
int temp = methodRandom();
listTest.Add(temp);
}
}
但是当我调试并单击按钮时,我收到以下错误消息,标记为“listTest.Add(temp);”说“NullReferenceException 未处理”。我做错了什么?
【问题讨论】:
标签: c# list random nullreferenceexception