私は宿題に取り組んでいます。私の課題は、ユーザーが選択したファイルをメモリに読み込むアプリケーションを作成することです。この場合、国と一致する略語を含む配列オブジェクトの構造を読み込んでいます。ファイルは正しく読み込まれ、アプリケーションを使用して国または略語で検索すると、ファイル内で一致する国または略語が検出され、適切に表示されます。ただし、情報を間違って入力し、一致するものが見つからない場合は、コードの 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