Error Message: Object reference not set to an instance of an objec

G

Guest

I have a parent form that launches its child form to a user to edit and
create input data for a parent form. Now, the problem is that after I closed
a child form and tried to call a routine of the parent form(class) to
redisplay and reflect the change I make, I got the error message "Object
reference not set to an instance of an object". Please see code below.

Private Sub ChildForm_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
Dim ParentForm As MainConsole
On Error GoTo Whoops

'// LoadDataGrid routine is basically used to display/refresh the
data on DataGrid
ParentForm.LoadDataGrid()

Whoops:
MsgBox("Unexpected Error:" & Err.Description)
Return
End Sub

I understand ParentForm is not an active form, therefore its routine is not
visible to ChildForm(different class). But, I simply do not want to use
"New" to initialize another instance of ParentForm and then end up having two
duplicate forms displaying to a user: one with the old data and one with the
current data. Let's say if that's the only way to do, and I then still do
not know how to close the very first ParentForm and have only its most
current instance remain displayed. Could someone shed some light for me on
this, please? Thank you so much for your time and insight.
 
C

Chris Dunaway

Private Sub ChildForm_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
Dim ParentForm As MainConsole

This ParentForm variable is Nothing because you didn't use New to
create the object.
ParentForm.LoadDataGrid()

Which is why this line gives you the exception.
I understand ParentForm is not an active form, therefore its routine is not
visible to ChildForm(different class). But, I simply do not want to use
"New" to initialize another instance of ParentForm and then end up
having two

In order to access the ParentForm, you need to pass in a reference to
the parent form. One way is to overload the constructor of the Chile
form so that it takes an instance of the parent form. Then you store
that reference in a local variable. Similar to this:

'This line somewhere near the top of your Child Form class
Private m_ParentForm As ParentForm

'Next overload the Sub New (constructor) to take an instance of the
parent form as an argument:

Public Sub New(parent As ParentForm)
m_ParentForm = parent
End Sub


'Finally, in the parent form
Public Sub SomeSub()
Dim MyChild As New ChildForm(Me) 'Pass in Me to the child
End Sub

With code like this, you can then refer to the parent form using the
Child's m_ParentForm variable.

There are other ways to do this, but this is a simple one. Hope this
gives you some ideas.

Chris
 
G

Guest

Thank you very much Chris. That's very helpful!

Chris Dunaway said:
This ParentForm variable is Nothing because you didn't use New to
create the object.


Which is why this line gives you the exception.


having two

In order to access the ParentForm, you need to pass in a reference to
the parent form. One way is to overload the constructor of the Chile
form so that it takes an instance of the parent form. Then you store
that reference in a local variable. Similar to this:

'This line somewhere near the top of your Child Form class
Private m_ParentForm As ParentForm

'Next overload the Sub New (constructor) to take an instance of the
parent form as an argument:

Public Sub New(parent As ParentForm)
m_ParentForm = parent
End Sub


'Finally, in the parent form
Public Sub SomeSub()
Dim MyChild As New ChildForm(Me) 'Pass in Me to the child
End Sub

With code like this, you can then refer to the parent form using the
Child's m_ParentForm variable.

There are other ways to do this, but this is a simple one. Hope this
gives you some ideas.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top