# Using a structure as elements of a jagged array

• ### Question

• Hi folks,

with the following code I get an error as shown in the picture. Can anyone suggest how to avoid getting the NullReferenceExceptioError. Thanks Bernie

```    Private Structure NNode
Dim NodeValue As Single
Dim Wieghts() As Single
End Structure

Sub example()
Dim Nodes(3)() As NNode
Nodes(0)(0).NodeValue = 5
Nodes(0)(0).Wieghts = {0.3, 0.5}
End Sub```

And I get the following error:

Any suggestions AAa

Bernie

Friday, September 27, 2019 3:42 AM

### All replies

• Hi Bernie Ok,

If it is one-dimension array, declare as follows,

Sub example()
Dim Nodes(3) As NNode
Nodes(0).NodeValue = 5
Nodes(0).Wieghts = {0.3, 0.5}
End Sub

If it is two-dimension array, declare as follows,

Sub example()
Dim Nodes(3, 3) As NNode
Nodes(0, 0).NodeValue = 5
Nodes(0, 0).Wieghts = {0.3, 0.5}
End Sub

Thanks and Best regards,

E-John

• Edited by Friday, September 27, 2019 4:48 AM
Friday, September 27, 2019 4:08 AM
• If you are interested in jagged arrays, then try ReDim. For example, if you want ten nodes in first row, and initialise the first one, then:

ReDim Nodes(0)(9)

Nodes(0)(0).NodeValue = 5

Nodes(0)(0).Weights = {0.3, 0.5}

If you want fours elements in second row:

ReDim Nodes(1)(3)

Do this for each row.

Friday, September 27, 2019 5:30 AM
• Hi,

You can try my code as follows:

```Public Class Form1
Example()
End Sub

Private Structure NNode
Dim NodeValue As Integer
Dim Wieghts() As Integer
End Structure

Sub Example()
Dim Nodes(3) As NNode
Nodes(1).NodeValue = 1
Nodes(1).Wieghts = {0.3, 0.5}
Nodes(2).NodeValue = 2
Nodes(2).Wieghts = {0.2, 0.3}
Nodes(3).NodeValue = 3
Nodes(3).Wieghts = {0.4, 0.6}

Dim a As NNode()() = New NNode(2)() {}
a(0) = New NNode() {Nodes(1)}
a(1) = New NNode() {Nodes(2), Nodes(3)}

End Sub
End Class```

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays.", and therefore its elements are reference types and are initialized to null.Each one of the array elements has a different size.

Also, you can read this document for further information.

Best Regards,

Julie

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Friday, September 27, 2019 6:58 AM
• Hi folks,

with the following code I get an error as shown in the picture. Can anyone suggest how to avoid getting the NullReferenceExceptioError. Thanks Bernie

```    Private Structure NNode
Dim NodeValue As Single
Dim Wieghts() As Single
End Structure

Sub example()
Dim Nodes(3)() As NNode
Nodes(0)(0).NodeValue = 5
Nodes(0)(0).Wieghts = {0.3, 0.5}
End Sub```

And I get the following error:

Any suggestions AAa

Bernie

Hi Bernie,

I know moderators have the order only to give answers 100% exactly as the OP asks. However, I see them give often very ancient solutions. Simply because of the fact that the original poster gives that ancient code. In my country we use the phrase for that "Problem solved, patient dead".

If you not direct create a complete empty fixed Jagged Array as some samples given in this thread do, then every time when you add a new row, you needs first to do a Redim as Viorel shows. In one of the samples is cleverly coded around the Redim, but it will never be usable in real world practise.

What a Redim does is recreate the whole old Array inside the new dimensions on a new place (not just references). The old Array will everytime be released by the Garbage Collector.

Why do you use that old method in '19 (or you would still have VB6 from 1998 as your tool)?

With a List in a List you have the same result with easier and more readable code.

Besides that you use a Single's which consumes exact the same memory as an integer and decimal does (a 32 bit computer can only address 4 bytes and a 64bit computer 8 bytes) whatever the used value type is. However, a Single can be very imprecise especial in business applications.

Here your problem coded with a generic list. (In a way you can test it direct in a module)

```Module Module1
Public NodeList As New List(Of NNodeRow)
Sub main()
Dim NodeR As New NNodeRow With {.Value = 5, .Wieghts = New List(Of Wiecht)}
NodeR.Wieghts.Add(New Wiecht With {.Item = 0.3})
NodeR.Wieghts.Add(New Wiecht With {.Item = 0.5})
Console.WriteLine(NodeList(0).Value)
Console.WriteLine(NodeList(0).Wieghts(0).Item)
Console.WriteLine(NodeList(0).Wieghts(1).Item)
End Sub
End Module
Public Class NNodeRow
Public Property Value As Integer
Public Property Wieghts As List(Of Wiecht)
End Class
Public Class Wiecht
Public Property Item As Decimal
End Class```

Success
Cor

Friday, September 27, 2019 10:57 AM
• Hi,

Do you resolve the issue? If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post.