Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27066

Null Reference Error

$
0
0
This has been driving me nuts. I keep getting a Null Ref error on the line H.Parts(1).PartName = "ROTOR".

The code shows no errors but at run time the error keeps popping up. I have tried implementing this using structures instead of classes but I get the same runtime error.

Can one of you knowledgeable coders tell me what's wrong and how to fix this?


Code:

Module Module1

    Public Class Helicopter

        Public Type As String = ""
        Public Reg As String = ""
        Public SN As Integer = 0
        Public Parts() As Part


        Public Class Part

            Public Property PartName As String
            Public Property HoursFlown As Single

        End Class

    End Class

    Sub Main()

        Dim H As New Helicopter
        Dim Fleet As New List(Of Helicopter)
        Dim Part As New Helicopter.Part


        H.Type = "AH-1"
        H.Reg = "N3456L"
        H.SN = 12345
        H.Parts(1).PartName = "ROTOR"
        H.Parts(1).HoursFlown = 472.9
        H.Parts(2).PartName = "HUB"
        H.Parts(2).HoursFlown = 302.7

        Fleet.Add(H)

        For Each Helicopter In Fleet
            Console.WriteLine(Helicopter.Type)
            For Each Part In Helicopter.Parts
                Console.Write(Part.PartName & " ") : Console.WriteLine(Part.HoursFlown)
            Next Part
        Next Helicopter


    End Sub

End Module


Viewing all articles
Browse latest Browse all 27066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>