Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

IComparer Object reference not set to an instance of an object.

I'm not familiar with IComparer, but I'm getting a null reference
What am I doing wrong?
Private _comparer As IComparer

                    If _comparer.Compare(_lastValue, e.Item.DataItem) <> 0 Then
                        'add a header if it was different from the previous item.

                        Dim item As New GroupHeader()

                        _groupTemplate.InstantiateIn(item)
                        item.DataItem = e.Item.DataItem
                        Me.Controls.Add(item)

                        item.DataBind()
                    End If
ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To be clear, the error you are getting is caused by the fact that you have not instantiated an object here:

Private _comparer As IComparer

To fix this normally in a declaration, you would add the New keyword and provide any constructor arguments necessary like so:

Private _comparer As New IComparer 'Invalid for IComparer

However, if you put this in code, you will see this will not work as IComparer is an Interface and cannot be instantiated; therefore, when you go to use it as an object, it is indeed a null reference since you cannot instantiate an object of that type in the first place.

Hopefully that makes sense.

HTH
Kevin
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

Thank you for the replies

_lastValue is object
e.Item.DataItem is System.Data.DataRowView

Should this class implement the IComparer interface in order to compare those two objects?
You can write a custom Comparer to compare objects that have special rules.  If you are just comparing the string values, then you can do this as string already implements Comparer.

e.Item(0).ToString().CompareTo(_lastValue)