显示表单时出现异常

时间:2015-07-20 22:24:46

标签: vb.net forms

当我运行程序并选择button8时,我得到:

  

创建表单时出错。请参阅Exception.InnerException   细节。错误是:对象引用未设置为的实例   对象

我的代码就像 -

一样简单
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    frequency.Show()
    Me.Close()
End Sub

错误发生在frequency.Show()上,但我不知道如何解决它,因为它只是打开了另一种形式。我已经检查了错误和异常但在运行之前没有显示任何内容。

编辑 - 在评论中添加了frequency.Load代码。

Dim LineOfText
Dim aryTextFile() As String
Dim Line As String
Using SR As New IO.StreamReader("...\mainmenu.txt")
    Do While Not SR.EndOfStream
        '________________
        LineOfText = My.Computer.FileSystem.ReadAllText("...\mainmenu.txt")
        aryTextFile = LineOfText.Split(",")
        For i = 0 To UBound(aryTextFile)
            'MsgBox(aryTextFile(i))
        Next i
        ' Read the file just created
        Line = SR.ReadLine
        TextBox1.Text = aryTextFile(0)
        TextBox2.Text = aryTextFile(1)
        TextBox3.Text = aryTextFile(2)
        TextBox4.Text = Today
        TextBox5.Text = aryTextFile(4)
    Loop
End Using

1 个答案:

答案 0 :(得分:0)

请在您的Load()事件中尝试此操作:

Public Class frequency

    Private Sub frequency_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim Lines() As String = System.IO.File.ReadAllLines("...\ MainMenu.txt")
            Dim aryTextFile() As String = Lines(0).Split(",")
            TextBox1.Text = aryTextFile(0)
            TextBox2.Text = aryTextFile(1)
            TextBox3.Text = aryTextFile(2)
            TextBox4.Text = Today
            TextBox5.Text = aryTextFile(4)
        Catch ex As Exception
            MessageBox.Show("Error Loading File")
        End Try
    End Sub

End Class