Object reference not set to an instance of an object.

adwaitjoshi

Active member
Joined
Dec 29, 2005
Messages
38
Programming Experience
1-3
I have added the following property to a Custom Form Control. However I get an error called "Object reference not set to an instance of an object." when I see the HasErrors property in the Property Window. I think it has to do with the ErrorMessages array and ErrorMessage.Length function because if i change ErrorMessages to a string variable and use If ErrorMessages="" Then I dont get any error. Please help.

#Region " Attributes "

#Region " Fields "

Public ErrorMessages() As String
#End Region 'Fields

#Region " Properties "

Public Property HasErrors() As Boolean
Get
If ErrorMessages.Length() > 0 Then
Return True
Else
Return False
End If
End Get
Set(ByVal Value As Boolean)

End Set
End Property


#End Region 'Properties

#End Region 'Atributes

2. How can I change the icon of the UICustomControl I am developing. Icon is the small icon that showsup in the toolbar. Right now it shows up as a "gear and a shaft" I want to change this to more appropriate icon.
 
if array is not initialized there is no length, try this
VB.NET:
If not ErrorMessages is nothing Then
Return True
 
A zero-length array is created like this:
VB.NET:
     Public ErrorMessages(-1) As String
Plus, It is not really a good idea to expose the array as a field like that. As it is a new array can be assigned to that field from outside the object, which is almost certainly undesirable. You should adopt the .NET convention and use a property:
VB.NET:
Private m_ErrorMessages(-1) As String

Public ReadOnly Property ErrorMessages() As String()
    Get
        Return Me.m_ErrorMessages
    End Get
End Property
The ReadOnly modifier prevents a new array being assigned from the outside. Your HasErrors property should also be ReadOnly because you can't logically set it externally. The Getter can also be more succinctly written:
VB.NET:
Public ReadOnly Property HasErrors() As Boolean
    Get
        Return Me.m_ErrorMessages.Length > 0
    End Get
End Property
 
Back
Top