-8

I got this error when running my program. I wasn't like this before when I'm creating this program.

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

Here is the code in the form :

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

Please help, guys.

J...
  • 28,957
  • 5
  • 61
  • 128
  • 8
    I've changed the title to something more appropriate since no-one really _cares_ how urgent you think it is - urgency is reserved for _paying_ clients :-) – paxdiablo Apr 12 '12 at 02:31
  • Creating what form? where the debug fired the error? this is sooooo easy to fix, if you debug the code! too much code -1 – gbianchi Apr 12 '12 at 02:34
  • 1
    This may sound obvious, but if you look at the inner exception it will likely tell you what error is not being set. This is almost certainly caused by not instantiating an object. If you don't know how to check the inner exception, which line is it crashing on? – vpiTriumph Apr 12 '12 at 02:36
  • By looking at your Inner Exception you should be able to find out what line your problem is on and from that what is causing your problem. – Mark Hall Apr 12 '12 at 02:36
  • 2
    You've posted dozens of lines of code consisting of `Something.Hide()`, and dozens of commented out lines of code, and a ton of unrelated code. Please edit your post to remove all the noise and junk, and reduce it to the minimum needed to demonstrate the problem, and then ask an actual question. – Ken White Apr 12 '12 at 02:39
  • You probably shouldn't post your database password to a public forum. – J... Apr 12 '12 at 08:13
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Oct 05 '14 at 19:10

1 Answers1

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

Here you are trying to initialize a string with a control property value. Class scoped variables (fields) are initialized before any other constructor code (ie: InitializeComponent()) is called, so here OpenFileDialog1 does not exist yet.

J...
  • 28,957
  • 5
  • 61
  • 128