Simon
Simon

Reputation: 6152

Why is Me.components Nothing?

I have written a custom ErrorProvider which adds some functionality to the existing ErrorProvider (sets control BackColor, ErrorCount etc). This was working find but now for some reason it falls over on the constructor:

_LoginErrorProvider = New ErrorLogErrorProvider(Me.components)

The error is a NullReferenceException which is caused by the fact that Me.components is Nothing. Can anyone shed any light on why a form's components collection would be Nothing? The form seems to work fine in every other way!

Upvotes: 0

Views: 1882

Answers (4)

Rachit Dhawan
Rachit Dhawan

Reputation: 1

Solved it!

Use below code to resolve it!

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

Upvotes: 0

Lonnie McCullough
Lonnie McCullough

Reputation:

You can also drop your ErrorLogErrorProvider class onto the design surface for your Form / UserControl and the code generated for InitializeComponent will correctly initialize the components member and pass it to the constructor of your error provider (VS does this for all non-visual components). Just make sure that your ErrorLogErrorProvider class derives from either Component or implements the IComponent interface.

Upvotes: 2

Hath
Hath

Reputation: 12769

when you add a component to the design surface it adds this in the InitializeComponent function

me.components = new System.ComponentModel.Container()

so just add this in your self.

or your

_LoginErrorProvider = New ErrorLogErrorProvider(Me.components)

is being called before InitializeComponent

Upvotes: 1

Simon
Simon

Reputation: 6152

Solved it, adding another component to the form seems to fix the problem, it's a bit of a cludge but works. I suppose the ideal solution would be to add my ErrorProvider to me.components but in order to do this you need to initialize a new instance which you can't because Me.components is Nothing!!

It could drive a man crazy.....

Upvotes: 0

Related Questions