Django Anonymous Django Anonymous - 5 months ago 18
Vb.net Question

system.NullReferenceException: Object not set to an instance of an object

Dim Permission As String
Dim chk As String = "p"
Permission = (ds.Tables("privilege").Rows(0).Item(0)).ToString
MessageBox.Show(Permission)
Dim PermissionArray() As String = Split(Permission, ":")
For i As Integer = 0 To 36
If PermissionArray(i) = 1 Then
Try
Dim chkBox As CheckBox = CType(Me.Controls(chk & i), CheckBox)
chkBox.Checked = True
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End If
Next


This code gives me the following error in catch, i have googled but nothing is working

This is the error:
System.NullReferenceException – Object reference not set to an instance of an object.

Answer

As you noted chkBox.Checked throws NullReferenceException, you should evaluate the following line for the error:

Dim chkBox As CheckBox = CType(Me.Controls(chk & i), CheckBox)

' This may throw NullReferenceException if there is no (chk & i) control available
chkBox.Checked = True
Comments