将项目添加到列表时出错

时间:2016-03-09 19:55:48

标签: vb.net project

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

    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)。 这是列表或其他东西? 请帮助。

2 个答案:

答案 0 :(得分:1)

你需要

Public entities As New List(Of Entity)

答案 1 :(得分:0)

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