我是使用类和OOP的新手。我创建了一个类,每次调用它时都会创建一个新标签。这是我的代码:
Public lbl As New Label
Public txt As New TextBox
Public controls As List(Of Control)
Public Sub New()
'Add a label
lbl.Name = "Label" & 1
lbl.Text = "Student " & 1 & ":"
lbl.Size = New Size(65, 20)
lbl.Location = New Point(10, (10 * 22) + 5)
controls.Add(lbl)
End Sub
当我打电话给这个班级时,我收到以下错误信息:
An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe Additional information: Object reference not set to an instance of an object.
消息突出显示的代码行是:
controls.Add(lbl)
任何帮助将不胜感激,谢谢。
答案 0 :(得分:1)
Public lbl As New Label
Public txt As New TextBox
Public controls As List(Of Control)
Public Sub New()
'Add a label
lbl.Name = "Label" & 1
lbl.Text = "Student " & 1 & ":"
lbl.Size = New Size(65, 20)
lbl.Location = New Point(10, (10 * 22) + 5)
If (controls is nothing) = True Then controls = new list(of Control)
controls.Add(lbl)
End Sub