Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 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