-1

Every time I run this code it shows me this message "nullreferenceexception was unhandled - Object reference not set to an instance of an object."

   Public Class Point3D
        Public X As Long = 0
        Public Y As Long = 0
        Public Z As Long = 0
    End Class


Public Structure TTriangle
            <VBFixedArray(2)> Public T2D() As Point2D
            <VBFixedArray(2)> Public VertT3D() As Point3D
            <VBFixedArray(2)> Public Real3DZ() As Long
            Public Color As Color
            Public IsDrown As Boolean
            Public IsSelected As Boolean
            Public IsIn As Boolean 'to draw or not??
            Public Sub initialize()
                ReDim T2D(2)
                ReDim VertT3D(2)
                ReDim Real3DZ(2)
                VertT3D(0) = New Point3D
                VertT3D(1) = New Point3D
                VertT3D(2) = New Point3D
            End Sub
End Structure


    ReDim Preserve Triangles(zTrianglesNo)
    Triangles(zTrianglesNo).VertT3D(0).X = Point1.X
    Triangles(zTrianglesNo).VertT3D(0).Y = Point1.Y
    Triangles(zTrianglesNo).VertT3D(0).Z = Point1.Z

How can i solve that? Thanks in advance.

Abuzanona
  • 27
  • 5
  • how are you declaring `Triangles`? you are not calling `initialize()`.`initialize()` is not constructor in vb.net if you mean constructor it is `Sub New()` – bansi Aug 21 '14 at 05:38

1 Answers1

0
ReDim Preserve Triangles(zTrianglesNo)
For i As Integer = 0 To Triangles.GetUpperBound(0) '< Added. Initialise the objects
  Triangles(i) = New TTriangle                     '< Added. Initialise the objects
  Triangles(i).initialize()                        '< Added. Initialise the objects
Next                                               '< Added. Initialise the objects
Triangles(zTrianglesNo).VertT3D(0).X = Point1.X
Triangles(zTrianglesNo).VertT3D(0).Y = Point1.Y
Triangles(zTrianglesNo).VertT3D(0).Z = Point1.Z

NOTE: This will reset all triangles in the array. If you are adding an element at a time, don't use the FOR...NEXT loop, just initialise the element you are adding.

SSS
  • 4,877
  • 1
  • 19
  • 39