Question Object reference not set to an instance of an object.

cookie

New member
Joined
Jul 22, 2012
Messages
3
Programming Experience
Beginner
Use the "new" keyword to create an object instance
Check to determine of the object is null before calling the method



i'm really starting to loose my patience with this error, i've searched and searched but somehow i can't can't find the right ones. >.<
will someone pleaaaseee~ please help me~

here is the code where the problem occurs, and the error points at the line that is Underlined.




VB.NET:
 Private Sub Save(ByVal Mode As String)
        If Mode = "Borrow" Then
            ans = MessageBox.Show("Are you sure you want to borrow the book?", "Important Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If ans = Windows.Forms.DialogResult.Yes Then


                strQry = "SELECT bookID,status FROM rc_book WHERE bookID = '" & Me.MaskedTextBox1.Text & "'"
[U][B]                    DTable = dl.CreateDataTableFromQry(strQry)[/B][/U]
                If DTable.Rows.Count <> 0 Then
                    For Each dRow As DataRow In DTable.Rows
                        strbookID = dRow("bookID")
                        strstatus = dRow("status")


                        If strbookID = Me.MaskedTextBox1.Text Then
                            strstatus = dRow("status")
                            If strstatus = "Unavailable" Then
                                MessageBox.Show("The book is unavailable")
                            ElseIf strstatus = "Lost" Then
                                MessageBox.Show("The book is lost")


                            ElseIf strstatus = "Damaged" Then
                                MessageBox.Show("The book is damaged")


                            Else




                                cmd = New MySqlCommand("INSERT INTO rc_borrow(bookID,title,status,studid,firstname,lastname,dateborrowed,datereturned,daystoborrow) VALUES ('" & strbookID & "','" & strtitle & "', '" & strstatus & "', '" & strid & "', '" & strfname & "', '" & strlname & "', '" & strdateborrow & "', '" & strdatereturn & "','" & strdays & "')", con)
                                If con.State = ConnectionState.Closed Then con.Open()
                                dr = cmd.ExecuteReader
                                MessageBox.Show("Record has been saved!")
                                con.Close()
                                Me.DataGridView1.Update()
                            End If
                        End If


                    Next
                End If


            End If
        End If
                        Me.DataGridView1.Refresh()


    End Sub


thank you in advance~
:D
 
A NullReferenceException occurs when you try to access an object that does exist. Here's an example:
Dim myObject As Object

MessageBox.Show(myObject.ToString())
That code declares a variable but assigns no object to the variable. You can't call the ToString method of an object that doesn't exist, so a NullReferenceException is thrown. One possible reason for this situation is that, just like this example, the developer declared the variable but forgot to create the object to assign to the variable:
Dim myObject As New Object

MessageBox.Show(myObject.ToString())
That code now invokes a constructor to create a new object and that is assigned to the variable.

So, when a NullReferenceException is thrown, the first thing to do is to check each reference on the line to see which one is Nothing. You can then backtrack to where you expected a value to be assigned. In your case the obvious candidate is 'dl'. Have you actually assigned a value to that variable? If not, that's your issue. If you have then you'll need to look further.
 
thanks for your reply, :)
but honestly, that line of code was just added by someone, and he didn't have time to explain to me what that line of code does, the 'dl' wasn't declared on anything so i thought it was alright, that the DataTable and the strqry was the problem. >.<
honestly, i don't know how to solve this anymore~
>.<
would you like to see all of the codes that comes with that?

and thank you again~
 
If the problem is a NullReeferenceException then I've told you how to solve it. If there's some other issue then please create a new thread for that issue and describe it in detail.
 
Back
Top