mardi 27 février 2018

(Vb.net) How to solve "Object reference not set to an instance of an object" error when using Reflection

everybody: How can I solve this "Object reference not set to an instance of an object" error when using Reflection. I'm a creating this "programme" to teach languages. Each Topic is in a separate form because it has many activities. To connect the list of activities to each topic (form) I'm trying to make this work: I'm trying to call one of these forms from a string which contains its name. I think the variable is already declared as a form. I also think the function helps the variable refering to a form. The name of the form is correct, too. So what do you think the problem is?

Private Sub Btn_EMPEZAR_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Btn_EMPEZAR.Click

  Dim strCreatedFromButton As String = "Frm_The_Greetings" 
  Dim frm As New Form

  frm = DirectCast(CreateObjectInstance(strCreatedFromButton), Form)

  frm.Show() 'HERE I GET THE "Object reference not set to an instance of an 
  'object" error ***********

End Sub


Public Function CreateObjectInstance(ByVal objectName As String) As Object

  Dim obj As Object

  Try

    If objectName.LastIndexOf(".") = -1 Then
      objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
    End If

    obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

  Catch ex As Exception
  obj = Nothing

  End Try
  Return obj

End Function





Aucun commentaire:

Enregistrer un commentaire