"The reference to the object is not installed in the instance of the object." Attempt to add to list

I created a list of email objects that contains an email object that has 3 parameters (String Address, String Subject, String Body)

Then I want to add to the list by creating more instances of "email". However, I tried so many different ways, and I don't have one.

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

If I press the button button1, I get the error "The reference to the object is not installed in the instance of the object."

Thank.

+5
source share
1 answer

You did not create an instance of the list, you only declared it.

 Public emails As New List(Of Email) 
 '                ^^^
+12
source

All Articles