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.