Skale
asked on
Object reference not set to an instance of object error in vb.net
Hello,
I'm using below code but i got an error as i wrote on title.
I'm pretty sure it causing from this section where i am finding tab control and try to color it's child datagridviews cell.
Any help would be grateful. I dont understand why it can't find control.
It can't find object wi
Full Code Block:
I'm using below code but i got an error as i wrote on title.
I'm pretty sure it causing from this section where i am finding tab control and try to color it's child datagridviews cell.
Any help would be grateful. I dont understand why it can't find control.
Check.StyleOutputDataGridView(CType(GUI.Controls.Find(tabName, True).FirstOrDefault, TabPage))
It can't find object wi
Full Code Block:
Public Shared Sub MeasureForceMarker()
Try
Dim forceList As List(Of IScrForce) = ModelBase.GetForceList(CType(Base.Model, IScrModelBase), True)
Dim tabCtrl As TabControl = CType(GUI.Controls.Find("tcCheckForceOut", True).FirstOrDefault, TabControl)
If forceList.Count > 0 Then
Dim tabName As String = "tpCheckForceOutMeas"
Dim dgvName As String = "dgvCheckForceOutMeas"
If Check.CheckTabPage(tabCtrl, tabName) = False Then
Dim tabpage As New TabPage With {
.Name = tabName,
.Text = "Measurements"
}
Dim dgv As New DataGridView With {
.Dock = DockStyle.Fill,
.Name = dgvName,
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None,
.AllowUserToResizeColumns = True,
.AllowUserToResizeRows = True,
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
.AllowUserToAddRows = False,
.RowHeadersVisible = False,
.[ReadOnly] = False
}
Dim colClass As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Class", .Frozen = True
}
Dim colName As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Name", .Frozen = True
}
Dim colDistX As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Dist. x [m]", .Tag = "m"
}
Dim colDistY As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Dist. y [m]", .Tag = "m"
}
Dim colDistZ As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Dist. z [m]", .Tag = "m"
}
Dim colDistM As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Dist. mag. [m]", .Tag = "m"
}
Dim colAngAl As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Ang. al [rad]", .Tag = "rad"
}
Dim colAngBe As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Ang. be [rad]", .Tag = "rad"
}
Dim colAngGa As New DataGridViewTextBoxColumn With {
.[ReadOnly] = True, .HeaderText = "Ang. ga [rad]", .Tag = "rad"
}
dgv.Columns.Add(colClass)
dgv.Columns.Add(colName)
dgv.Columns.Add(colDistX)
dgv.Columns.Add(colDistY)
dgv.Columns.Add(colDistZ)
dgv.Columns.Add(colDistM)
dgv.Columns.Add(colAngAl)
dgv.Columns.Add(colAngBe)
dgv.Columns.Add(colAngGa)
tabpage.Controls.Add(dgv)
tabCtrl.TabPages.Add(tabpage)
Else
Dim existTp As TabPage = DirectCast(tabCtrl.Controls(tabName), TabPage)
Dim existDgv As DataGridView = DirectCast(existTp.Controls(dgvName), DataGridView)
existDgv.Rows.Clear()
End If
For Each force In forceList
Dim res() As String = EvalForceMarkerPosition(force)
Dim tp As TabPage = DirectCast(tabCtrl.Controls(tabName), TabPage)
Dim dgv As DataGridView = DirectCast(tp.Controls(dgvName), DataGridView)
dgv.Rows.Add(GetClass(CType(force, ScrNamedObject)), force.name, res(0), res(1), res(2), res(3), res(4), res(5), res(6))
Next
Check.StyleOutputDataGridView(CType(GUI.Controls.Find(tabName, True).FirstOrDefault, TabPage))
End If
Catch ex As Exception
Tools.OutputLog.Print(Tools.OutputLog.Type.ERR, ex.Message)
End Try
ASKER
Hi ste5an,
As you can see on the codeblock i'm using that tab and adding some controls before, and i'm sure it's existing but i don't know why it's not give error for that part.
As you can see on the codeblock i'm using that tab and adding some controls before, and i'm sure it's existing but i don't know why it's not give error for that part.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm trying with below code and it gives a message it's exists
I don't understand really.
Dim test As TabPage = CType(Controls.Find("tpCheckForceOutMeas", True).Single, TabPage)
If Not test Is Nothing Then
MsgBox("Exists!")
Else
MsgBox("Not Exists!")
End If
I don't understand really.
On what hierarchy are you now? In your OP you were on GUI.Controls. Now you're on the active form. Is it the same as GUI?
ASKER
The problem occurs from a different procedure calling with that now it's solved thank you ste5an.
Open in new window
btw, .FirstOrDefault is not necessary. Cause a name must be unique in opposite to e.g. a caption or tag.When you're sure that the control must exist, then you need to inspect your object hierarchy at runtime. Maybe it does exist, but is in a different scope not covered by GUI.Controls.Find(tabName,