创建表单时出现“对象引用未设置”错误

时间:2012-04-12 02:28:13

标签: vb.net

运行程序时出现此错误。在我创建这个程序之前,我不是这样的。

An error occurred creating the form.
See Exception.InnerException for details.
The error is:
    Object reference not set to an instance of an object.

以下是表单中的代码:

Imports System.Data
Imports System.Data.OleDb

Public Class IndexFrm

#Region "Connection"

    Dim con As OleDbConnection
    Dim scmd As OleDbCommand
    Dim conreader As OleDbDataReader
    Dim dbcon As String = Me.OpenFileDialog1.FileName & ";Jet Oledb:Database Password=*****"

#End Region

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        FrmSynoyms.Show()
        FrmAntonyms.Hide()
        FrmAnalogy.Hide()
        FrmMath.Hide()
        FrmAbstract.Hide()
        FrmAbstract2.Hide()
        FrmAbstract3.Hide()
        FrmAbstract4.Hide()
        StdntsFrm.Hide()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        FrmAntonyms.Show()
        FrmAnalogy.Hide()
        FrmSynoyms.Hide()
        FrmMath.Hide()
        FrmAbstract.Hide()
        FrmAbstract2.Hide()
        FrmAbstract3.Hide()
        FrmAbstract4.Hide()
        StdntsFrm.Hide()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        FrmAnalogy.Show()
        FrmAntonyms.Hide()
        FrmSynoyms.Hide()
        FrmMath.Hide()
        FrmAbstract.Hide()
        FrmAbstract2.Hide()
        FrmAbstract3.Hide()
        FrmAbstract4.Hide()
        StdntsFrm.Hide()
    End Sub

    Private Sub StudentsFormToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StudentsFormToolStripMenuItem1.Click
        StdntsFrm.Show()
        FrmAnalogy.Hide()
        FrmAntonyms.Hide()
        FrmSynoyms.Hide()
        FrmMath.Hide()
        FrmAbstract.Hide()
        FrmAbstract2.Hide()
        FrmAbstract3.Hide()
        FrmAbstract4.Hide()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        FrmMath.Show()

        FrmAnalogy.Hide()
        FrmAntonyms.Hide()
        FrmSynoyms.Hide()
        FrmAbstract.Hide()
        FrmAbstract2.Hide()
        FrmAbstract3.Hide()
        FrmAbstract4.Hide()
        StdntsFrm.Hide()
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        FrmAbstract.Show()
        FrmAntonyms.Hide()
        FrmAnalogy.Hide()
        FrmMath.Hide()
        StdntsFrm.Hide()
    End Sub

    'Private Sub IndexFrm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    '    If e.KeyCode = Keys.Escape Then
    '        Try
    '            If Me.WindowState = FormWindowState.Minimized Then
    '                Me.WindowState = FormWindowState.Minimized
    '                NotifyIcon1.Visible = True
    '                Me.Hide()
    '            End If
    '        Catch ex As Exception
    '            MsgBox(ex.Message)
    '        End Try
    '    End If
    'End Sub

    Private Sub IndexFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolStripButton1.Enabled = False
        Me.ToolStripButton2.Enabled = False
        Me.ToolStripButton3.Enabled = False
        Me.ToolStripButton4.Enabled = False
        Me.ToolStripButton5.Enabled = False
        Me.StudentsFormToolStripMenuItem1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Label3.Text = Val(Me.Label3.Text) - 1

        If Me.Label1.Text = "0" And Me.Label2.Text = "0" And Me.Label3.Text = "0" Then
            Me.Timer1.Stop()
            Me.Timer1.Enabled = False
            MsgBox("TIME IS UP")

            SynonymsSave()
            AntonymsSave()
            AnalogySave()
            MathSave()
            AbstractSave()

            StdntsFrm.TxtStdntName.Clear()
            StdntsFrm.TxtStdntsMI.Clear()
            StdntsFrm.TxtStdntsLast.Clear()
            StdntsFrm.TxtStdntAdd.Clear()
            StdntsFrm.TxtStdntSchool.Clear()
            StdntsFrm.TxtSchoolAdd.Clear()
            StdntsFrm.TxtStdntAdv.Clear()
            StdntsFrm.StdntTel.Clear()

            StdntsFrm.Show()
        Else
            If Me.Label3.Text = "0" And Me.Label2.Text <> "0" Then
                Me.Label3.Text = "59"
                Me.Label2.Text = Val(Me.Label2.Text) - 1
            ElseIf Me.Label2.Text = "0" And Me.Label1.Text <> "0" Then
                Me.Label2.Text = "2"
                Me.Label1.Text = Val(Me.Label1.Text) - 1
            ElseIf Me.Label1.Text = "0" Then
                Me.Label1.Text = "0"
            ElseIf Me.Label2.Text = "0" And Me.Label1.Text = "0" Then
                Me.Label2.Text = "0"
            End If
        End If
    End Sub

    Private Sub StudentsFormToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmDBPath.Show()
    End Sub

    Private Sub IndexFrm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.WindowState = FormWindowState.Minimized
                NotifyIcon1.Visible = True

                Me.Hide()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Try
            Me.Show()
            Me.WindowState = FormWindowState.Normal

            NotifyIcon1.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub OpenDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenDatabaseToolStripMenuItem.Click
        Dim DBpath As String

        With OpenFileDialog1
            .AddExtension = True
            .CheckPathExists = True
            .DefaultExt = ".mdb"
            .DereferenceLinks = True
            .Filter = "Access File (*.mdb)|*.mdb|All files|*.*"
            .Multiselect = False
            .RestoreDirectory = True
            .ShowHelp = True
            .ShowReadOnly = True
            .Title = "Select file to open"
            .ValidateNames = True

            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Try
                    DBpath = My.Computer.FileSystem.ReadAllText(.FileName)
                    Me.StudentsFormToolStripMenuItem1.Enabled = True
                Catch ex As Exception
                    MsgBox("THE DATABASE IS ALREADY BEING USED", MsgBoxStyle.Exclamation)
                End Try
            End If
        End With
    End Sub

    Sub SynonymsSave()
        con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
        con.Open()

        Dim qstring As String = "Insert into tblStdntsScores (StdntName,Synonyms,ScoreDate) values ('" & FrmSynoyms.Label59.Text & "','" & FrmSynoyms.Label58.Text & "','" & StdntsFrm.Label13.Text & "')"
        scmd = New OleDbCommand(qstring, con)
        scmd.ExecuteReader()

        scmd.Dispose()
        con.Close()

        FrmSynoyms.HistorySave()
    End Sub

    Sub AntonymsSave()
        con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
        con.Open()

        Dim qstring As String = "Update tblStdntsScores set Antonyms='" & FrmAntonyms.Label28.Text & "' where StdntName='" & FrmAntonyms.Label29.Text & "'"
        scmd = New OleDbCommand(qstring, con)
        scmd.ExecuteReader()

        scmd.Dispose()
        con.Close()

        FrmAntonyms.HistorySave()
    End Sub

    Sub AnalogySave()
        con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
        con.Open()

        Dim qstring As String = "Update tblStdntsScores set Analogy='" & FrmAnalogy.Label308.Text & "' where StdntName='" & FrmAnalogy.Label368.Text & "'"
        scmd = New OleDbCommand(qstring, con)
        scmd.ExecuteReader()

        scmd.Dispose()
        con.Close()

        FrmAnalogy.HistorySave()
    End Sub

    Sub MathSave()
        con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
        con.Open()

        Dim qstring As String = "Update tblStdntsScores set Math='" & FrmMath.Label67.Text & "' where StdntName='" & FrmMath.Label68.Text & "'"
        scmd = New OleDbCommand(qstring, con)
        scmd.ExecuteReader()

        scmd.Dispose()
        con.Close()

        FrmMath.HistorySave()
    End Sub

    Sub AbstractSave()
        con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
        con.Open()

        Dim qstring As String = "Update tblStdntsScores set Abstract='" & FrmAbstract4.Label36.Text & "' where StdntName='" & FrmAbstract.Label36.Text & "'"
        scmd = New OleDbCommand(qstring, con)
        scmd.ExecuteReader()

        scmd.Dispose()
        con.Close()

        FrmAbstract4.HistorySave()
    End Sub
End Class

请大家帮帮忙。

1 个答案:

答案 0 :(得分:2)

Dim dbcon As String = Me.OpenFileDialog1.FileName & "...etc"

在这里,您尝试使用控件属性值初始化字符串。类范围变量(字段)在调用任何其他构造函数代码(即:InitializeComponent())之前初始化,因此此处OpenFileDialog1尚不存在。