Inherited Form with error Object Reference not set...

  • Thread starter Thread starter Giovanni Bassi
  • Start date Start date
G

Giovanni Bassi

Hello All,

I have encountered a problem.
I am using visual inheritance and my base form adds an event handler on Form
Load using the AddHandler Keyword.

The problem is that if the Event Handler code is there, when I create the
inherited form I get the error "Object Reference not set to an instance of
an object". If it is not I get no error.
I have tried leaving this code in the forms Sub New, but it produces the
same error.
I need the handler to be added during the form load and removed on the form
unload. I cannot use the withevents keyword because the class is declared on
the global level, and it is actually a property of this globally declared
class. Some code to help:


-//-

On a global Module:
Friend g_objTables As TableOps

On the application's Sub Main:
g_objTables = New TableOps(AppSettings.ConnectionString)

On the Base Class:
Private Sub frmBase1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
AddHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub

Private Sub CostCentersDataChanged() 'handling DataChanged Event

'various steps taken to handle the event

End Sub

Private Sub RangeChanged()

'various steps taken to handle the event

End Sub

Private Sub frmBase1_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed

RemoveHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
RemoveHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged

End Sub


On the Derived Class:
Private Sub frmCostCenters_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

m_btObj = g_objTables.CostCenters

End Sub

-//-



If I comment the two code lines in frmBase1_Load I get no error. If I
don't...
Also, if I try calling something else, like Msgbox("Test"), it works fine.

Does anyone know why that's happening? Can't I add event handlers in the
Form Load?

Thanks a lot in advance for all the replies.

Giovanni Bassi
 
i usually add handlers in sub new() after mybase.new() and
initializecomponent()...i haven't had problems. btw, you could try this in
your form class:

private withevents m_btObj as TableOps.CostCenters = g_objTables.CostCenters
' or set it to g_objTables in your mybase.load delegate.

that should work just fine...your code w/b:

delegateFunctionBlahBlahBlah(...) handles m_btObj.Blah()

that way you can leave out the explicit handers (add/remove) and let the
framework handle the delegate resources.

hth,

steve

btw...you'd be better off removing the handlers on the closing event rather
than the closed event. the reason for the errors, i think, is b/c the base
class isn't instanciated yet...let it load b4 making use of it's interfaces.
 
Hey Steve,

Thanks for the explanation. I tried your way but it wouldn't work either.
I added a

If Me.DesignMode = False Then
AddHandler m_btObj.DataChanged, AddressOf CostCentersDataChanged
AddHandler m_btObj.Range.RangeChanged, AddressOf RangeChanged
End If

to the form's load event. I can see the form now and everything works fine.
I believe the problem was in handling the m_btObj object. It is set to

m_btObj = g_objTables.CostCenters

on the derived form only. And CostCenters is a property of g_objTables that
returns a reference to an object. That object is only created at that point,
when the reference is requested, and that is probably why the IDE could not
design the whole thing, because it could not find the object. At run time
everything was working fine, so I thought of the DesignMode property, which
came quite handy.

Giovanni Bassi
 
Back
Top