我遇到了一个大问题。我尝试从一个文件加载数据到列表框,但是当我加载它时,我在文件的最后一行得到了一个异常。它说“值不能为空。参数名称:项目”。 我用这个代码
ListBox2.Items.Clear() 'clear listbox2
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim read_text As New StreamReader(ListBox1.Items.Item(i).ToString, System.Text.Encoding.GetEncoding(1250)) ' listbox1.items.item(i) is the path of file I load data from
Try
Do While read_text.Peek >= 0
If read_text.ReadLine.ToString.Contains(":") Then 'dont load lines without ":" mark
ListBox2.Items.Add(read_text.ReadLine)
End If
Loop
read_text.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next i
问题出在哪里?有人可以帮忙吗? ;)
答案 0 :(得分:1)
ListBox.Items.Add see the MSDN documentation抛出异常。您不能在ListBox.Items
中添加null(或VisualBasic中没有任何内容)同样如评论中所写,你读了一行并用contains检查,但是读了下一行把它添加到列表中。
您应该更改代码,以便在变量中保存从 ReadLine 获得的行。然后检查它是否包含“:” - 在这种情况下,您可以将变量添加到ListBox2.Items。