NullReferenceException未处理VB.NET

时间:2010-11-27 19:11:22

标签: vb.net search nullreferenceexception

我正在尝试为数组分配一条记录,为了搜索条件的原因,该记录必须拆分为更多元素。例如,如果条件设置为First Name,则在数组元素0中搜索匹配关键字,因为这是在当前所选记录为Split()之后放置名字的位置。

搜索表单的来源:

Public Class frmSearch
    Dim arrayCurrentRecord(6) As String
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = DialogResult.Cancel
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        'fill array with list box items
        Dim arraySearch(frmMain.numberOfRecords) As String
        For i = 0 To frmMain.numberOfRecords - 1
            arraySearch(i) = frmMain.lstListBox.Items(i)
        Next i

        If rbtnFirstName.Checked = True Then
            For i = 0 To UBound(arraySearch)
                arrayCurrentRecord = arraySearch(i).Split(" ")
                If InStr(arrayCurrentRecord(0), txtSearch.Text) = True Then
                    lstSearch.Items.Add(arraySearch(i))
                End If
            Next i
            MsgBox("search complete")
        End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

For i = 0 To UBound(arraySearch)更改为 For i = 0 To UBound(arraySearch) - 1解决了这个问题,但它找不到任何只是说搜索完成的内容。

答案 1 :(得分:-1)

更改行

If InStr(arrayCurrentRecord(0), txtSearch.Text) = True

If InStr(arrayCurrentRecord(0), txtSearch.Text)

解决了这个问题。