0
我正在做我的功課。我的任務是創建一個應用程序來讀取用戶選擇到內存中的文件。在這種情況下,它正在讀取包含國家和它的匹配縮寫的數組對象的結構。文件加載正確,當我使用應用程序按國家或縮寫進行搜索時,它會在文件中找到匹配的國家或縮寫,並按照它的顯示方式進行顯示。但是,如果我輸入的信息不正確,而且找不到匹配項,則應將其發送到If Not Found代碼塊並顯示一個消息框。相反,它崩潰並拋出一個異常,說:VB.NET異常問題
對象引用未設置爲對象的實例。
我不明白髮生了什麼事。請仔細查看我的代碼,如果可以,請協助。謝謝。
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
你能告訴我們它崩潰什麼線?逐行進入您的代碼並根據需要設置斷點。 – ApplePie
當我輸入無效信息dewpending選擇哪個單選按鈕時,它崩潰在「Found = Country(Counter).Abbreviation.ToUpper = txtAbbrev.Text.ToUpper」或「Found = Country(Counter).Names.ToUpper = txtCountry .Text.ToUpper「行 –