ListBox.Items.Clear未将对象引用设置到对象的实例
写了一个日志窗口logForm,包含一个ListBox显示日志内容,当日志满200行的清除所有内容,但是报错,提示未将对象引用设置到对象的实例代码如下:
if (this.logForm.listBoxLog.Items.Count > 200)
this.logForm.listBoxLog.Items.Clear(); --------------------编程问答-------------------- 单步调试一下,看看那个是null。
目测是this.logForm.listBoxLog.Items --------------------编程问答-------------------- 不是时时都出现,软件一直运行,都是晚上十一点多,或者凌晨一点多出现错误 --------------------编程问答--------------------
[目测是this.logForm.listBoxLog.Items]去掉 --------------------编程问答-------------------- 都是深夜这么奇怪?是否记录与时间跨度相关的东西了? --------------------编程问答-------------------- 目测是内存的问题 --------------------编程问答-------------------- this.logForm.listBoxLog.Items.Clear();这句话有没有执行呢,按照道理上面已经加了判断了,不会报错的。 --------------------编程问答-------------------- 测试没有任何问题 --------------------编程问答-------------------- 而且还纳闷地一点是,先判断了数量大于200行才执行的,应该不会null啊 --------------------编程问答-------------------- 日志记录的内容,和时间跨度没有什么关系
this.logForm.listBoxLog.BeginUpdate();
this.logForm.listBoxLog.Items.Add(DateTime.Now.ToString() + ":" + logContent);
this.logForm.listBoxLog.EndUpdate();
this.logForm.listBoxLog.SelectedIndex = this.logForm.listBoxLog.Items.Count - 1; --------------------编程问答-------------------- 现在又出现了这种问题,不知道哪里有问题 --------------------编程问答-------------------- this.logForm.listBoxLog.SelectedIndex = this.logForm.listBoxLog.Items.Count - 1;
这句有问题。 COUN -1 . INDEX. --------------------编程问答-------------------- 这里确实会引发错误,但是错误日志提示的位置不在这里
private void OutPutLogs(LogLevel logLevel, string logContent)
{
lock (this)
{
switch (logLevel)
{
case LogLevel.Info:
this.logForm.listBoxLog.BeginUpdate();
this.logForm.listBoxLog.Items.Add(DateTime.Now.ToString() + ":" + logContent);
this.logForm.listBoxLog.EndUpdate();
if (this.logForm.listBoxLog.Items.Count > 0)
this.logForm.listBoxLog.SelectedIndex = this.logForm.listBoxLog.Items.Count - 1;
break;
default:
break;
}
}
}
补充:.NET技术 , C#