Array with the New keyword

Suriv

Active member
Joined
Jul 16, 2007
Messages
33
Programming Experience
1-3
I try to store some values through this sub in objPointList.
But, with the following code i receive an error: "Object reference not set to an instance of an object".

VB.NET:
Private Sub UpdateGraph(ByVal YValue As Double, ByVal intPListIndex As Integer)
Dim PPDrawList As New ZedGraph.PointPairList
        Static objPointList(System.Byte.MaxValue) As ZedGraph.PointPairList
        Dim X As Double

        If objPointList(intPListIndex).Count <> 0 Then
            X = objPointList(intPListIndex).Item(objPointList(intPListIndex).Count - 1).X
        Else
            X = -1
        End If

        objPointList(intPListIndex).Add(X + 1, YValue)
            PPDrawList.Add(X + 1, YValue)

            zgcNetwork.GraphPane.AddBar("", PPDrawList, Color.DarkRed)
            zgcNetwork.Refresh()
End Sub

So i tried "Static objPointList(System.Byte.MaxValue) As New ZedGraph.PointPairList", but it doesn't work too: then i get an error about an array which cannot be declared with the new keyword.

Any help would be appreciated :)


PS i hope i posted this in the right forum...
 
When you create an array of reference types each element is initially a null reference. Think of it like an egg carton with no eggs. You've got twelve cups in the egg carton but none of them contains an egg until you put one in there. The same is true of an array. To fill an array you have to do something like this:
VB.NET:
Dim eggCarton(12 - 1) As Egg

For i As Integer = 0 To 12 - 1 Step 1
    eggCarton(i) = New Egg
Next i
You can create an array with the New key word, but not like that. Here are the different ways you can create an array:
VB.NET:
Dim arr1(upperBound) As Object
Dim arr2 As Object() = New Object(upperBound) {}
Dim arr3 As Object() = New Object() {obj1, obj2, obj3}
Dim arr4 As Object() = {obj4, obj5, obj6}
Dim arr5 As Object()
ReDim arr5(upperBound)
 
I tried your different ways to declare the array, but i still get the error "Object reference not set to an instance of an object". :(

Is the solution to the "Object reference not set to an instance of an object" error creating an array with the new keyword?

If no, what do i need to do to solve the problem? :)
 
An array is just a collection of variables of same type, so instead of writing this to create an array for two items of type Egg:
VB.NET:
Dim TwoEggs(1) As Egg
you could have written
VB.NET:
Dim EggVariable1 As Egg
Dim EggVariable2 As Egg
Either way you still have to assign an object instance to the variable before you can use it when the type is a reference type:
VB.NET:
EggVariable1 = New Egg
TwoEggs(0) = New Egg
This is where your problem lies, you have declared the array but the array doesn't contain any items yet. If you want to use the 123rd slot of the objPointList (obj?) array you have to assign a new instance of PointPairList to it first.
 
If you want to use the 123rd slot of the objPointList (obj?) array you have to assign a new instance of PointPairList to it first.
OK, i've used this kind of code, and it seems to work:
VB.NET:
For i As Integer = 0 To 12 - 1 Step 1
    eggCarton(i) = New Egg
Next i

Thanks :)
 
Back
Top