我运行这段代码来做一个活动日志窗口。
Sub writetolog(i As String)
Try
'outfile.Write(DateTime.Now.ToString("mm/dd/yyyy - H:mm:ss:fffffff") & "--->" & i & vbCrLf)
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:ff") & " - " & i)
If String.IsNullOrWhiteSpace(i) = False Then LogLB.Items.Add(DateTime.Now.ToString("[" & "MM/dd/yy - HH:mm:ss:ff") & "] -- " & i) 'Else MsgBox(i & " is nothing!")
If LogLB.Items.Count >= 100 Then LogLB.Items.RemoveAt(0)
If LogLB.Items.Count > 0 Then LogLB.SelectedIndex = LogLB.Items.Count - 1
Catch ex As Exception
Console.Write(ex)
End Try
End Sub
所以它大部分时间都有效,但出于某些原因,在看似随机的时间我得到一个Null Exception并指向该行的结尾
LogLB.Items.Count - 1
为什么?