Results 1 to 6 of 6

Thread: [RESOLVED] Object reference not set to an instance of an object.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    10

    Resolved [RESOLVED] Object reference not set to an instance of an object.

    When trying to display data from the database into my textboxes, this error keeps appearing: “Object reference not set to an instance of an object.”
    I don’t know how to fix it, and not that fabulous at coding,so i know there is probably major issues, but i just want to know how to solve this problem first.


    Code:
    Public Class FrmTeach_SelectQuestions
        Dim Inc As Integer
        Dim MaxRows As Integer
        Dim Teach_Question As New DataSet
        Dim dsNewRow As DataRow
        Dim SQL As String
    
        Private Sub FrmTeach_SelectQuestions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Teach_Question = DataBaseConnection.DBSelect("SELECT [QuestionText], [Unit], [A1], [A2], [A3], [A4], [CorrectAnswer] FROM [UnitQuestionsTable];")
    
            MaxRows = Teach_Question.Tables("Results").Rows.Count
            Inc = -1
            DataBaseConnection.ResetDS()
            Reset()
    
        End Sub
        Private Sub NavigateRecords()
            TxbQuestionText.Text = Teach_Question.Tables("Results").Rows(Inc).Item(1)
            TxbA1.Text = Teach_Question.Tables("Results").Rows(Inc).Item(3)
            TxbA2.Text = Teach_Question.Tables("Results").Rows(Inc).Item(4)
            TxbA3.Text = Teach_Question.Tables("Results").Rows(Inc).Item(5)
            TxbA4.Text = Teach_Question.Tables("Results").Rows(Inc).Item(6)
        End Sub
    
        Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
            If Inc <> MaxRows - 1 Then
                Inc = Inc + 1
                NavigateRecords()
            Else
                MsgBox("...")
            End If
        End Sub
    
        Private Sub BtnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrevious.Click
            If Inc > 0 Then
                Inc = Inc - 1
                NavigateRecords()
            ElseIf Inc = -1 Then
                MsgBox("No Records Yet")
            ElseIf Inc = 0 Then
                MsgBox("First Record")
            End If
        End Sub
    
        Private Sub BtnMoveLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMoveLast.Click
            If Inc <> MaxRows - 1 Then
                Inc = MaxRows - 1
                NavigateRecords()
            End If
        End Sub
    
        Private Sub BtnMoveFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMoveFirst.Click
            If Inc <> 0 Then
                Inc = 0
                NavigateRecords()
            End If
        End Sub
    
        Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
            'Teach_Question.Tables("Results").Rows(Inc).Item(1) = TxbQuestionText.Text
           
            SQL = "INSERT INTO UnitQuestionsTable ([QuestionText], [Unit], [A1], [A2], [A3], [A4], [CorrectAnswer]) " & "VALUES ('" & TxbQuestionText.Text & "', '" & CmbUnit.Text & "', '" & TxbA1.Text & "', '" & TxbA2.Text & "', '" & TxbA3.Text & "', '" & TxbA4.Text & "', '" & CmbCorrectAnswer.Text & "')"
            DataBaseConnection.DBUpdate(SQL)
            MsgBox("New Updates Saved.")
        End Sub
    
        Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
    
            If Inc <> -1 Then
    
                dsNewRow = Teach_Question.Tables("Results").NewRow()
    
                SQL = "INSERT INTO UnitQuestionsTable ([QuestionText], [Unit], [A1], [A2], [A3], [A4], [CorrectAnswer]) " & "VALUES ('" & TxbQuestionText.Text & "', '" & CmbUnit.Text & "', '" & TxbA1.Text & "', '" & TxbA2.Text & "', '" & TxbA3.Text & "', '" & TxbA4.Text & "', '" & CmbCorrectAnswer.Text & "')"
                DataBaseConnection.DBUpdate(SQL)
    
                Teach_Question.Tables("Results").Rows.Add(dsNewRow)
    
                MsgBox("New Question Added To The Database")
    
            End If
        End Sub
    End Class
    Can anyone please try and help me fix it, thanks.
    any additional information can be provided.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Object reference not set to an instance of an object.

    Which line is causing the error? Also, what is "DataBaseConnection".

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Object reference not set to an instance of an object.

    When a NullReferenceException is thrown, it means that you tried to access a member of an object that doesn't exist. The first thing to do is to look at the call stack of the exception to see what line the exception was actually thrown on. If it was a line in your own code then the the IDE will have highlighted that line. If it was a line in compiled code then the IDE will have highlighted the last line of your own code that was executed. In the first case, which is the more likely, you simplay have to test the value of each reference on the line to see which one is Nothing. You can then work backwards to see where you expected a value to be assigned.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    10

    Re: Object reference not set to an instance of an object.

    the error appears here
    Code:
    TxbQuestionText.Text = Teach_Question.Tables("Results").Rows(Inc).Item(1)
    database connection is the module used to connect to the database, i don't there is anything wrong there though

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Object reference not set to an instance of an object.

    As I said, a NullReferenceException occurs when you try to access a member of an object that doesn't exist. As I also said, once you've identified the line the exception is thrown on, you simply need to determine which reference is Nothing. A reference is basically anything before a dot, because the dot is what you use to specify that you want to access a member. So, when the exception occurs, which reference on that line is Nothing? Once you've identified that, where did you expect a value to be assigned to that reference?

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    10

    Re: Object reference not set to an instance of an object.

    Thanks, it works now
    i had been using data set previously on another form, i guess i needed to reset the dataset.
    i did this by placing the 'resets' before my command line

    Code:
    DataBaseConnection.ResetDS()
     Reset()
    Teach_Question = DataBaseConnection.DBSelect("SELECT [QuestionText], [Unit], [A1], [A2], [A3], [A4], [CorrectAnswer] FROM [UnitQuestionsTable];")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width