将项目添加到列表时出错

error with adding item to list

我正在尝试使用 vb 制作游戏,所以我制作了这个 class:

    Imports Microsoft.VisualBasic.PowerPacks
    Module EntityI
    Public entities As List(Of Entity)
    Public Function getEntity(ByVal uuid As Guid) As Entity
    For i = 0 To entities.Count - 1
        If entities.Item(i).getUUID = uuid Then
            Return entities.Item(i)
        End If
    Next
    Return Nothing
End Function
    End Module
    Public Class Entity
Private uuid As Guid
Private location As Location
Private shape As OvalShape

Public Sub New()
    uuid = System.Guid.NewGuid
    Dim canvas As New PowerPacks.ShapeContainer
    canvas.Parent = Game
    shape = New OvalShape With {.Parent = canvas}
    shape.SetBounds(50, 50, 50, 50)
    save()
End Sub
Public Function getUUID() As Guid
    Return uuid
End Function
Public Function getLocation() As Location
    Return location
End Function
Public Sub teleport(ByVal location As Location)
    Me.location = location
End Sub
Private Sub save()
entities.add(Me)
End Sub
End Class

所以这会在 entities.add(Me) (NullRefernceException) 处产生错误。 是列表中的东西还是其他东西? 请帮忙

你需要

Public entities As New List(Of Entity)

你是新名单。您只是将变量声明为列表。将 Public entities As List(Of Entity) 更改为 Public entities As New List(Of Entity)