?
Solved

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

Posted on 2009-07-01
5
Medium Priority
?
399 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 60

Accepted Solution

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

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 1000 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 60

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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