1

I'm having a bit of a problem. Granted, I'm not the best with VB, but I can't figure out for the life of my why this isn't working.

I have an Access database that I pull data from. I'm trying to dynamically create labels so I can present the data as prettily as I want. However, when trying to execute it, I'm getting an exception "System.NullReferenceException: Object reference not set to an instance of an object" on the line arrayLabels(i).Text = "Howdy"

I'm almost certain it's something stupidly simple that I'm just missing... but here's my code:

Private Sub TabControl2_Click(sender As Object, e As EventArgs) Handles TabControl2.Click, btnRefresh1.Click, btnRefresh2.Click, btnRefresh3.Click, ddSelectTech.SelectedIndexChanged, ddSelectTech2.SelectedIndexChanged

    If TabControl2.SelectedIndex = 0 Then

        'use this to count the number of rows
        Dim numRows As Integer

        'here be database stuff
        Dim da2 As OleDb.OleDbDataAdapter
        Dim ds2 As New DataSet
        Dim con2 As New OleDb.OleDbConnection
        con2.ConnectionString = dbProvider & dbSource
        sqlStatusOpen = "SELECT * FROM work_orders WHERE status = 'In Progress';"

        da2 = New OleDb.OleDbDataAdapter(sqlStatusOpen, con2)
        con2.Open()
        da2.Fill(ds2, "installations2")
        con2.Close()
        numRows = ds2.Tables("installations2").Rows.Count()

        'create an array label based on the number of rows in the table
        Dim arrayLabels(numRows) As Label

        'loop it to actually make the labels, position them, and such
        For i = 0 To (numRows - 1) 'just looping the number of rows
            Dim x As Integer = 100
            Dim y As Integer = 1 + (i * 10)
            Try
                TabPage3.Controls.Add(arrayLabels(i))
                arrayLabels(i).Text = "Howdy"
                arrayLabels(i).Location = New Point(x, y)
            Catch ex As Exception
                MessageBox.Show(ex.ToString, "Looky there, Franky, another error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        Next

    ElseIf TabControl2.SelectedIndex = 1 Then

    ElseIf TabControl2.SelectedIndex = 2 Then

    Else

    End If

End Sub

Of course, if you think there are better ways to handle this, I'm open to suggestions.

1 Answer 1

4

Your code:

Dim arrayLabels(numRows) As Label

Creates an array with the type Label. But every entry in this array is null.

Try to initialize every label in your loop:

Dim label As New Label
label.Text = "Howdy"
label.Location = New Point(x, y)
TabPage3.Controls.Add(label)

And you don't need to save the labels inside an array.

Sign up to request clarification or add additional context in comments.

3 Comments

in fact, first google hit was msdn.microsoft.com/en-us/library/vstudio/…
Oh holy crap was I overthinking the bleep outta that... the logic was just outside of my reach. This was one of those "lessons learned hard"...
@jparnell8839, Rather than putting the label's location to a specific point, put them in a FlowLayoutPanel in the tab page? Something like FlowLayoutPanel.controls.Add(newLabel)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.