Solved

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

Posted on 2009-07-01
5
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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