Solved

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

Posted on 2009-07-01
5
390 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:JRockFL
  • 3
5 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 24757896
IComparer is an interface that should be implemented by a class to allow it be comparable.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24757939
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
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 24757960
Hi JRockFL;

in your code snippet you have the following:

Private _comparer As IComparer

If _comparer.Compare(_lastValue, e.Item.DataItem) <> 0 Then

_comparer is a variable of type IComparer, which is an interface and can't be instantiated directly. Either you have a class that implements a customer Comparer or you cast an object that implemented a IComparer interface. The reason you are getting the error is that you do not have an instance of _comparer. What is the type of _lastValue and e.Item.DataItem?

Fernando
0
 
LVL 8

Author Comment

by:JRockFL
ID: 24757996
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?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24758067
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)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question