2011-12-19 96 views
5

我創建了包含具有3個參數的「email」對象的對象「emails」列表(String Address,String Subject,String Body)「對象引用未設置爲對象的實例」嘗試添加到列表

然後我想通過創建更多的「email」實例來添加到列表中。但是,我嘗試了很多不同的方式,而且沒有。

Public Class Test 

    Public emails As List(Of Email) 

    Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

     emails(0).setAddress("Hello") 
     emails(0).setSubject("World2") 
     emails(0).setBody("Why don't you work?") 
     emails.Add(New Email("Hello2", "World2", "Why don't you work?2")) 

    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Label1.Text = emails(0).getAddress 
     Label2.Text = emails(0).getSubject 
     Label3.Text = emails(0).getBody 

     Label4.Text = emails(1).getAddress 
     Label5.Text = emails(1).getSubject 
     Label6.Text = emails(1).getBody 
    End Sub 
End Class 

如果我點擊button1,我得到錯誤「對象引用未設置爲對象的實例」。

謝謝。

回答

12

您尚未創建列表的實例,只聲明瞭它。

Public emails As New List(Of Email) 
'    ^^^ 
相關問題