VB.NET异常问题

时间:2012-10-06 19:42:52

标签: vb.net exception loops

我正在做作业。我的任务是创建一个应用程序,读取用户在内存中选择的文件。在这种情况下,它读取的数组对象结构包括一个国家和它的匹配缩写。文件正确加载,当我使用应用程序按国家或缩写搜索时,它会在文件中找到匹配的国家或缩写,它会按原样显示它。但是,当我输入错误的信息并且找不到匹配时,应该转到If Not Found代码块并打开一个消息框。相反,它会崩溃并抛出一个异常,说:

对象引用未设置为对象的实例。

我不明白发生了什么。如果可以,请查看我的代码并提供帮助。谢谢你。

Option Strict On

'import a file
Imports System.IO

Public Class frmCountry

'Module Structure
Structure Countries
    Dim Names As String
    Dim Abbreviation As String
End Structure

'module array
Dim Country(257) As Countries
'file reader
Private sr As StreamReader

'search button click event
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    'event level variables
    Dim Found As Boolean
    Dim Counter As Integer

    'looks for entry match
    If rdoAbbrev.Checked = True Then
        Do Until Found Or Counter > 256
            Found = Country(Counter).Abbreviation.ToUpper = txtAbbrev.Text.ToUpper
            If Found Then
                txtCountry.Text = Country(Counter).Names
            Else
                Counter += 1
            End If
        Loop
    Else
        Do Until Found Or Counter > 256
            Found = Country(Counter).Names.ToUpper = txtCountry.Text.ToUpper
            If Found Then
                txtAbbrev.Text = Country(Counter).Abbreviation
            Else
                Counter += 1
            End If
        Loop

    End If
    'match not found response
    If Not Found Then
        MessageBox.Show("This is not a valid entry.", "NO MATCH FOUND", MessageBoxButtons.OK)
        If rdoAbbrev.Checked = True Then
            txtAbbrev.Text = ""
            txtAbbrev.Focus()
        Else
            txtCountry.Text = ""
            txtCountry.Focus()

        End If
    End If
    'reset variables
    Counter = 0
    Found = False
End Sub
'exit button click event
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
End Sub
'clear button click event
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    rdoAbbrev.Checked = False
    rdoCountry.Checked = False
    txtAbbrev.Text = ""
    txtAbbrev.ReadOnly = True
    txtCountry.Text = ""
    txtCountry.ReadOnly = True
    lblAbbrev.Visible = False
    lblName.Visible = False
    lblTitle.Focus()

End Sub
'radio button selected event
Private Sub rdoCountry_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoCountry.CheckedChanged, rdoAbbrev.CheckedChanged
    Dim ButtonSelected As RadioButton

    ButtonSelected = CType(sender, RadioButton)

    Select Case ButtonSelected.Name
        Case "rdoAbbrev"
            lblName.Text = "Matching Name:  "
            txtCountry.Text = ""
            lblName.Visible = True
            txtCountry.ReadOnly = True
            lblAbbrev.Text = "Enter Abbreviation:  "
            lblAbbrev.Visible = True
            txtAbbrev.ReadOnly = False
            txtAbbrev.Focus()

        Case Else
            lblName.Text = "Enter Name:  "
            lblName.Visible = True
            txtCountry.ReadOnly = False
            txtCountry.Focus()
            lblAbbrev.Text = "Matching Abbreviation:  "
            lblAbbrev.Visible = True
            txtAbbrev.ReadOnly = True
            txtAbbrev.Text = ""
    End Select


End Sub
'user uses browse button to select file
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
    Dim DialogueResponse As DialogResult
    Dim IndexInteger As Integer

    If sr IsNot Nothing Then
        sr.Close()
    End If

    With OpenFileDialog1
        .InitialDirectory = Directory.GetCurrentDirectory
        .FileName = "Country.txt"
        .Title = "Select File or Directory for File"
        DialogueResponse = .ShowDialog
    End With

    If DialogueResponse <> DialogResult.Cancel Then
        sr = New StreamReader(OpenFileDialog1.FileName)
    End If

    Do Until sr.Peek = -1
        If IndexInteger <= 256 Then
            Country(IndexInteger).Abbreviation = sr.ReadLine
            Country(IndexInteger).Names = sr.ReadLine
            IndexInteger += 1
        Else
            Exit Do
        End If
    Loop
    sr.Close()
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

显然抛出了异常,因为数组和循环被设置为搜索比包含的文件更多的对象。

此问题已得到解决

-solved -