Hey guys,

I have a form that is used to navigate through records in a table one at a time. The form generates the number of textboxes required automatically. Navigating through the records is easy enough, but when I click the update button it comes up with the error:

Object reference not set to an instance of an object.

Here is the code I have for when the Update button is clicked:

Dim con As New MySqlConnection
Dim da As New MySqlDataAdapter
Dim Query As New MySqlCommand

For i As Integer = 0 To intNoColumns - 1
    strUpdate &= l(i).Text & "='" & t(i).Text & "', "
Next

strUpdate = strUpdate.Substring(0, strUpdate.Length - 2)
strUpdate &= " WHERE " & strPrimaryKey & "=" & l(intPrimaryKeyIndex).Text

con.ConnectionString = frmLogin.strconn

con.Open()

Query.Connection = con
Query.CommandText = strUpdate
Query.ExecuteNonQuery()

con.Close()

Me.Close()
frmStart.Show()

The error comes up in the for loop. How can I get around this error?

Thanks in advance for any help :)

What is the error and what happens when you step through this in the debugger?

You might also want to wrap your code in try/catch blocks.

When I step through the debugger it gets to the for loop at the top (which is used to get the label text and textbox text to create the query) and on its first time through the loop it comes up with the error NullReferenceException was unhandled. Object reference not set to an instance of an object.

I think it may be to do with how the textbox and labels are generated and created? But I'm not sure. Here is the code I use to create text boxes when the form loads:

Dim l(intNoColumns) As Label
Dim t(intNoColumns) As TextBox

Private Sub CreateTextBoxes()

        Dim intylocation As Integer = 45

        GetPrimaryKey()

        For i As Integer = 0 To intNoColumns - 1

            l(i) = New Label
            l(i).Name = "lblField" & i.ToString
            l(i).Location = New Point(25, intylocation)
            l(i).Size = New Size(100, 15)
            l(i).Text = dt.Columns(i).ToString
            Me.pnlResults.Controls.Add(l(i))

            t(i) = New TextBox
            t(i).Name = "txtField" & i.ToString
            t(i).Location = New Point(150, intylocation)
            t(i).Size = New Size(300, 10)
            t(i).Text = dt.Rows(intCurrentRecord).Item(i)

            If l(i).Text = strPrimaryKey Then
                t(i).Enabled = False
                intPrimaryKeyIndex = i
            End If

            Me.pnlResults.Controls.Add(t(i))

            intylocation += 50

            ReDim l(intNoColumns), t(intNoColumns)

        Next

    End Sub

Like I said in my original post, they load fine and with the correct information. It doesn't get to the part where it actually executes the query, so I think it may be to do with how they are set up to begin with.

Thanks for your replies

Here is a rewritten version of your code using ArrayLists (better when dynamicly adding/removing from a list/array)

Dim t As New ArrayList
Dim l As New ArrayList

   For i = 0 To intNoColumns - 1
        Dim lbl As New Label
        Dim txt As New TextBox

        lbl.Name = "lblField" & i
        lbl.Location = New Point(25, intylocation)
        lbl.Size = New Size(300, 15)
        lbl.Text = dt.Columns(i).ToString
        Me.pnlResults.Controls.Add(lbl)

        txt.Name = "txtField" & i
        txt.Location = New Point(150, intylocation)
        txt.Size = New Size(300, 10)
        txt.Text = dt.Rows(intCurrentRecord).Item(i)

        If lbl.Text = strPrimaryKey Then
            txt.Enabled = False
            intPrimaryKeyIndex = i
        End If
        Me.pnlResults.Controls.Add(txt)

        intylocation += 50

        t.Add(txt)
        l.Add(lbl)
    Next

This will be must easier for you to use than using redim for each new item in the array.

I forgot to add, to retrieve the object from the ArrayList, it's good practice to do the following.

Dim txtBox As TextBox = TryCast(t(i), TextBox)
Dim lbl As Label = TryCast(l(i),Label)

If the cast fails, they will return Nothing.

So by just checking to ensure they are not nothing, you will safeguard from null references.

I think that l(i) and t(i) don't exist. You've named your objects as txtField & i.
Try to cast "txtField" & i in textbox and get it's value this way.

This works perfectly! Just had time to test it before I got away. Thank you :).

I just have one more question, I'm away from the server now so I can't test it really but I have some navigation buttons at the bottom which are supposed to go to the next record (changing the intCurrentRecord to make the text in the boxes change). Originally I had it so it was creating a new textbox which went over the original displaying the result of intCurrentRecord. If I change the code to this:

    Private Sub UpdateRowValue()

        Dim intylocation As Integer = 45

        For i As Integer = 0 To intNoColumns - 1
            Dim txt As New TextBox

            txt.Name = "txtField" & i
            txt.Location = New Point(150, intylocation)
            txt.Size = New Size(300, 10)
            txt.Text = dt.Rows(intCurrentRecord).Item(i)

            If i = intPrimaryKeyIndex Then
                txt.Enabled = False
            End If

            Me.pnlResults.Controls.Add(txt)
            txt.BringToFront()

            intylocation += 50

            t.Add(txt)
        Next

    End Sub

The code that calls this function changes the intCurrentRecord to the next or previous one. Would this work? Or is there an easier way now that they've been changed into ArrayLists?

Thank you so much for your help :). It's much appreciated

It would work.

If you wanted to remove it later you could try:

t.RemoveAt(t.IndexOf(txtField4))

If I put t.RemoveAt(t.IndexOf(txtField(intCurrentRecord - 1)) just above the add line would that get rid of the one which is being shown to begin with?

Actually, I've just seen that won't work how I'm thinking it would in my head. Nevermind about that :), as long as it works well enough for now :).

Thank you so much for your help :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.