"object instance not set to an instance of an object vb.net" happens when switch form

i have these 2 lines of code causing problem "object instance not set to an instance of an object vb.net" :
              
                dim frm2 as new frm2
                frm2.DsGetAllQs.Clear()
                frm2.SqlDataAdapterGetAllQs.Fill(frm2.DsGetAllQs)


i have frmStart, frm1, and frm2.
In frmStart, i have a btnStart contains code: frm1.show, me.hide
In frm1, i have the code above to be executed when itself is shown. when first time the btnStart is clicked, the code in frm1 execute fine. also on frm1 i have btnGoToFrm2 which contains code frm2.show, me.hide
In frm2, i have a btnReturn contains code: frm1.show, me.hide. when i click on btnReturn, frm2 is hide and frm1 is shown.
Now if i click on btnGoToFrm2 on frm1 again, a message shows: "object instance not set to an instance of an object vb.net". beside this message, everything will still work as expected.
I located the error source, which is the code on the top. Can anyone help plz
btw, the DsGetAllQs and SqlDataAdapterGetAllQs are visually dropped on to frm2 in design view

i checked everything, from my limited knowledge, i cant figure it out, plz help
However, nothing is actually affected by the error message, so... if you cant figure it out either, maybe you can suggest a way to hide the messge

frm2 code (full) and frm1 code (partial), the frm2 is actually called frmTesting1, frm1 is frmTestList
on datamodule(moduleUser), i have:
Public frmTestList1 As New frmTestList
Public frmTesting1 As New frmTesting

i dont know how to post a txt file with my code here.......






Answer this question

"object instance not set to an instance of an object vb.net" happens when switch form

  • Mr pinchy

    hi Sunder, I failed to email the zipped project to u, seems that it's rejected. plz indicate another way that i can pass it to u.

    regards!



  • Mark Tomlinson - MSFT

    hi KathTam:

    i use (VS 2002/2003), it seems to be the problem of the dataset, becasue the error occurs on the line of dataset.clear()



  • bennykfc

    thx very very much kathtam, here is the code in both forms that might be causing the problem, the blue colored code is my identified problem area:

    frm1:

    ---------------------------------------------------------------------------------------------------------

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Try
                ModuleUser.frmTesting1.Show()

                'initialize time elements for result
                iniDateTimes()

                If (isTest = True) And (ModuleUser.curTest <> Nothing) Then     'if is a test then start test
                    startTest()  'start current test
                Else
                    'if not a test, gives error
                    MsgBox("No test selected" & vbNewLine & "Please selecte a test!")
                End If

            Catch Exp As SqlClient.SqlException
                MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

            Catch Exp As Exception
                MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
            End Try

        End Sub

        Private Sub iniDateTimes()

            'initialize time elements for result
            If ModuleUser.startDate <> Nothing Then
                ModuleUser.startDate = Nothing
            End If

            If ModuleUser.startTime <> Nothing Then
                ModuleUser.startTime = Nothing
            End If

            If ModuleUser.endDate <> Nothing Then
                ModuleUser.endDate = Nothing
            End If

            If ModuleUser.endTime <> Nothing Then
                ModuleUser.endTime = Nothing
            End If

        End Sub

        Private Sub startTest()

            Try
                If Not SqlConnectionTest.State = ConnectionState.Open Then
                    SqlConnectionTest.Open()
                End If
                'check if test has questions or not
                SqlCommandDoesTestHaveQ.Parameters("@TNAME").Value = ModuleUser.curTest

                If SqlCommandDoesTestHaveQ.ExecuteScalar() = 0 Then
                    'if test doent have questions, deny testing
                    MessageBox.Show("Testing Denied!" & vbNewLine & "Test is empty, please add questions to the test first!", "Testing", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    'if test has questions, start testing
                    'get frmTesting1 caption
                    ModuleUser.frmTesting1.Text = "Testing:   " & ModuleUser.curTest

                    'execute the test
                    If ModuleUser.frmTesting1.SqlConnectionTesting.State = ConnectionState.Closed Then
                        ModuleUser.frmTesting1.SqlConnectionTesting.Open()
                    End If
                    'get current TID for reference
                    ModuleUser.frmTesting1.SqlCommandGetTID.Parameters("@TNAME").Value = ModuleUser.curTest

                    'get total number of questions for the label
                    ModuleUser.frmTesting1.SqlCommandTotalQs.Parameters("@TID").Value = ModuleUser.frmTesting1.SqlCommandGetTID.ExecuteScalar().ToString()
                    ModuleUser.frmTesting1.lblTotalQs.Text = ModuleUser.frmTesting1.SqlCommandTotalQs.ExecuteScalar().ToString()

                    'fill question textbox with first record
                    ModuleUser.frmTesting1.SqlCommandGetQDetails.Parameters("@TID").Value = ModuleUser.frmTesting1.SqlCommandGetTID.ExecuteScalar().ToString()
                    ModuleUser.frmTesting1.RTBQuestion.Text = ModuleUser.frmTesting1.SqlCommandGetQDetails.ExecuteScalar().ToString()

                    'empty answer textbox
                    ModuleUser.frmTesting1.RTBAnswer.Text = Nothing

                    'get full marks for current test and fill into lblFullMarks.text
                    ModuleUser.frmTesting1.SqlCommandFullMarks.Parameters("@TID").Value = ModuleUser.frmTesting1.SqlCommandGetTID.ExecuteScalar().ToString()
                    ModuleUser.frmTesting1.lblFullMarks.Text = ModuleUser.frmTesting1.SqlCommandFullMarks.ExecuteScalar().ToString()


                    'fill the questions list
                    ModuleUser.frmTesting1.SqlSelectCommandGetAllQs.Parameters("@TID").Value = ModuleUser.frmTesting1.SqlCommandGetTID.ExecuteScalar().ToString()

                    '----------------------------------------
                    'No problem before this point
                    '----------------------------------------

                    ModuleUser.frmTesting1.DsGetAllQs.Clear()

                    ModuleUser.frmTesting1.SqlDataAdapterGetAllQs.Fill(ModuleUser.frmTesting1.DsGetAllQs)

                    '----------------------------------------
                    'problem is before this point'
                    '----------------------------------------


                    'display make-up question numbers in question number list
                    ModuleUser.frmTesting1.ListBoxQNum.Items.Clear()
                    Dim i As Integer
                    For i = 1 To ModuleUser.frmTesting1.SqlCommandTotalQs.ExecuteScalar().ToString()
                        ModuleUser.frmTesting1.ListBoxQNum.Items.Add(i)
                    Next

                    'get UID for reference
                    ModuleUser.frmTesting1.SqlCommandGetUID.Parameters("@UNAME").Value = ModuleUser.userName

                    'add new result for the testing
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@TID").Value = ModuleUser.frmTesting1.SqlCommandGetTID.ExecuteScalar().ToString()
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@UID").Value = ModuleUser.frmTesting1.SqlCommandGetUID.ExecuteScalar().ToString()
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@STARTDATE").Value = Now.ToLongDateString()
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@STARTTIME").Value = Now.ToLongTimeString()
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@ENDDATE").Value = Now.ToLongDateString()
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@ENDTIME").Value = Now.ToLongTimeString()
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@TOTALMARKS").Value = 0
                    ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@GRADE").Value = "FAILED"
                    ModuleUser.frmTesting1.SqlCommandAddResult.ExecuteNonQuery()

                    'get current RID for reference
                    ModuleUser.frmTesting1.SqlCommandGetCurRID.Parameters("@STARTTIME").Value = ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@STARTTIME").Value
                    ModuleUser.frmTesting1.SqlCommandGetCurRID.Parameters("@ENDTIME").Value = ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@ENDTIME").Value
                    ModuleUser.curResultID = ModuleUser.frmTesting1.SqlCommandGetCurRID.ExecuteScalar().ToString()

                    'get time elements' values for result
                    ModuleUser.startDate = ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@STARTDATE").Value
                    ModuleUser.startTime = ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@STARTTIME").Value
                    ModuleUser.endDate = ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@ENDDATE").Value
                    ModuleUser.endTime = ModuleUser.frmTesting1.SqlCommandAddResult.Parameters("@ENDTIME").Value

                    ModuleUser.frmTesting1.SqlConnectionTesting.Close()

                    Me.Hide()

                    SqlConnectionTest.Close()
                End If

            Catch Exp As SqlClient.SqlException
                MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

            Catch Exp As Exception
                MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
            End Try

        End Sub

    ---------------------------------------------------------------------------------------------------



  • flinxperbil

    You can ignore the exception by doing the following

    Dim frm2 As New frm2

    Try

    frm2.DsGetAllQs.Clear()

    frm2.SqlDataAdapterGetAllQs.Fill(frm2.DsGetAllQs)

    Catch ex As System.NullReferenceException

    'ignore

    End Try

    However- it would be nice to understand why the code is throwing the Exception in the first place.  You mention that you can't post the text file here so I need to know a bit more information (Besides the explaination you gave above)

    Exactly which line again, was the error occuring on   it's possible that you need to wait for Form2 (frm2) to finish initializing and if that is the case, I can help supply some code to do this.

    thanks in advance,

     



  • Render

    thx Sunder, It's very kind of u :)

    now I am using .net2005, i converted the project in .net2005, however, yes, the problem still persists, I "grayed out" the buttons so that this function is not accessable by users (what a silly way)

    plz look at frmResult and frmTesting, there are 2 pink-colored buttons with the same problem.

    btw, sorry for the messy code, it's my first vb.net project :)

    the db file is also attached, you can either copy the db file or recover from the backup device. I found recover from backup device is easier.

    plz comment on any stupid code.

    I am trying to send the zipped project along with the db through to ur email adress, hope they can reach u, thx again.



  • Chris Clements

    frm2:


    ---------------------------------------------------------------------------------------------------

       Private Sub btnGiveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGiveUp.Click
            If MsgBox("Are you sure to quit this test ", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                giveUpCurrentTest()
            End If
        End Sub

        Private Sub giveUpCurrentTest()
            Try
                If SqlConnectionTesting.State = ConnectionState.Closed Then
                    SqlConnectionTesting.Open()
                End If
                'confirm with user "are you sure to give up "
                'if yes, then remove current result and all atqs


                MessageBox.Show("Testing terminated!", "Give up test", MessageBoxButtons.OK, MessageBoxIcon.Information)

                'remove all ATQs
                SqlDeleteCommandAllATQs.Parameters("@RID").Value = ModuleUser.curResultID
                SqlDeleteCommandAllATQs.ExecuteNonQuery()

                'remove current result
                SqlCommandRemoveCurResult.Parameters("@RID").Value = ModuleUser.curResultID
                SqlCommandRemoveCurResult.ExecuteNonQuery()

                ModuleUser.curResultID = Nothing
                ModuleUser.startDate = Nothing
                ModuleUser.startTime = Nothing
                ModuleUser.endDate = Nothing
                ModuleUser.endTime = Nothing

                'return to test list
                ModuleUser.frmTesting1.Show()
                Me.Hide()
                ModuleUser.frmTestList1.iniFormTestList()

                SqlConnectionTesting.Close()

            Catch Exp As SqlClient.SqlException
                MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

            Catch Exp As Exception
                MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
            End Try
        End Sub

     

        Private Sub frmTesting_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            If (MessageBox.Show("Are you sure to quite the program ", "Closing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel) Then
                'dont want to quite, stay still
                e.Cancel = True

            ElseIf (MsgBox("Are you sure to quit this test ", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
                'decide to give up the test and quite
                giveUpCurrentTest()
                Application.Exit()
            Else
                'dont want to quite, stay still
                e.Cancel = True
            End If
        End Sub

     
    ------------------------------------------------------------------------------------------------

    btw, i applied the catch clause you provided, however, the error msg still shows.

    thx again for ur patience and help



  • Orf Quarenghi

    if i eventually cant fix the problem, can anyone help me on totally ignore the error message i.e. stop the message popping up at runtime.



  • Latso

    Hello

    Are you still having the problem Is it possible to zip your project and send it to us

    Thanks

    Sunder

    VB.Net


  • Luke B

    Hello

    Please email it to my Yahoo acct sundetnv@yahoo.com

    Thanks

    Sunder

    VB.net


  • BMONSTER

     
    the "SqlDataAdapterGetAllQs " is visually dropped on the frmTesting in design view, how come frm1 doenst recognize it  
     
    I tried this:
    Dim SqlDataAdapterGetAllQs1 As new ModuleUser.frmTesting.SqlDataAdapterGetAllQs

    It gave me this: type ModuleUser.frmTesting.SqlDataAdapterGetAllQs is not defined.

    What can i do now if the object SqlDataAdapterGetAllQs is created on frmTesting visually just like a button or a textbox, it should stay there all the time when the frmTesting exists.

  • IvanP

       ModuleUser.frmTesting1.SqlDataAdapterGetAllQs.Fill(ModuleUser.frmTesting1.DsGetAllQs)


    this line is causing the problem exactly. but  dont know why........

  • Quirk

    Hi there

    Could you let me know if you're using the new TableAdapter (2005) or if you are using the old SqlDataAdapter (VS 2002/2003)

    There could be a possiblity that your SqlDataAdapter is not configured properly.  You can verify this by bringing up the Preview Dialog at design time and then filling in the dataset...

    Let me know

    thanks!



  • "object instance not set to an instance of an object vb.net" happens when switch form