Cannot initialize array

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
I'm wondering if somebody could help me resolve the infamous ""Object reference not set to an instance of an object." when I try to execute the following:

I have a class like the following:
Visual Basic:
Partial Public Class BinaryTreeSrchCrit

    Private intFldNum As Integer
    Private strSrchValue As String
    Private strOperator As String
    Private intSrchID As Integer
    Private intGrpIdx As Integer
    Private intPropIdx1 As Integer
    Private intPropIdx2 As Integer
    Private strLogicalOperator As String
    Private intRelIdx As Integer
    Private blnTrVal As Boolean
    Private strLRNode As String
    Private tnUlink As BinaryTreeSrchCrit 'top node of the tree
    Private tnLlink As BinaryTreeSrchCrit 'left node of the tree
    Private tnRlink As BinaryTreeSrchCrit 'right node of the tree

    Public Property FldNum() As Integer
        Get
            Return intFldNum
        End Get
        Set(ByVal Value As Integer)
            intFldNum = Value
        End Set
    End Property

    Public Property SrchValue() As String
        Get
            Return strSrchValue
        End Get
        Set(ByVal Value As String)
            strSrchValue = Value
        End Set
    End Property
.
.
.

In my app, I have the following code to try and populate the class:
Visual Basic:
Dim btSrchCrit(tblSrchCrit.Rows.Count - 1) As BinaryTreeSrchCrit
                For i = 0 To tblSrchCrit.Rows.Count - 1
                    If Not tblSrchCrit.Item(i).IsFldNumNull Then
                        btSrchCrit(i).FldNum = tblSrchCrit.Item(i).FldNum
                    End If
                    If Not tblSrchCrit.Item(i).IsGrpIdxNull Then
                        btSrchCrit(i).GrpIdx = tblSrchCrit.Item(i).GrpIdx
                    End If
                    If Not tblSrchCrit.Item(i).IsLogicalOperatorNull Then
                        btSrchCrit(i).LogicalOperator = tblSrchCrit.Item(i).LogicalOperator
                    End If
                    If Not tblSrchCrit.Item(i).IsPropIdx1Null Then
                        btSrchCrit(i).PropIdx1 = tblSrchCrit.Item(i).PropIdx1
                    End If
                    If Not tblSrchCrit.Item(i).IsPropIdx2Null Then
                        btSrchCrit(i).PropIdx2 = tblSrchCrit.Item(i).PropIdx2
                    End If
                    btSrchCrit(i).RelIdx = tblSrchCrit.Item(i).RelIdx
                    btSrchCrit(i).SrchID = tblSrchCrit.Item(i).SrchID
                    If Not tblSrchCrit.Item(i).IsSrchValueNull Then
                        btSrchCrit(i).SrchValue = tblSrchCrit.Item(i).SrchValue
                    End If
                    If Not tblSrchCrit.Item(i).Is_OperatorNull Then
                        btSrchCrit(i)._Operator = tblSrchCrit.Item(i)._Operator
                    End If
                Next

It's giving me the error on the above line:
Visual Basic:
btSrchCrit(i).FldNum = tblSrchCrit.Item(i).FldNum

The following is from my debugging session:
Code:
btSrchCrit	{Length=1}	clsParseCDR.BinaryTreeSrchCrit()
		(0)	Nothing	clsParseCDR.BinaryTreeSrchCrit
		btSrchCrit(i)	Nothing	clsParseCDR.BinaryTreeSrchCrit
		i	0	Short
As you can see, there is 1 btSrchCrit object created (since all I have in this case is one record coming into my function).
The 0th index is Nothing. This is why the error is happening, but I don't know how to resolve it. There is a value in "tblSrchCrit.Item(i).FldNum" before it tries to assign it to the property in the array.
After creating an index (Dim btSrchCrit(tblSrchCrit.Rows.Count - 1) As BinaryTreeSrchCrit), I thought that the 0th entry in the array would not be Nothing. You can't create arrays with a "New" keyword.

Can somebody please tell me how I can resolve this issue?

Thanks so much....
 
Last edited by a moderator:
When you create an array of reference types (classes) you do not get the elements themselves created - every element is nothing until you store something in it.
 
Back
Top