0

我正在做我的家庭作业。我的任务是创建一个将用户选择的文件读入内存的应用程序。在这种情况下,它正在读取包含国家和匹配缩写的数组对象结构。文件正确加载,当我使用应用程序按国家或缩写搜索时,它会在文件中找到匹配的国家或缩写,并按应有的方式显示。但是,当我错误地输入信息并且找不到匹配项时,它应该转到 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
4

1 回答 1

1

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

此问题已得到解决

-解决了-

于 2012-10-06T20:15:46.833 回答