Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5845927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:30:16+00:00 2026-05-22T12:30:16+00:00

I am trying to implement a sorted list. I have created the class I

  • 0

I am trying to implement a sorted list. I have created the class I want to have stored in the list, but for some reason when I try and run the sort the sort method I get an exception thrown. It appears something behind the scenes is passing in a null reference into the CompareTo method I have implemented.

Here’s what I believe to be relevant code.

Data object is defined like this:

namespace PrioritisedRequestQueue
{
public class XactTask : IComparable<XactTask>
{
    public int priority;
    public DateTime submitted;
    public string name;

    public XactTask( int priority, DateTime submitted, string name)
    {
        this.priority = priority;
        this.submitted = submitted;
        this.name = name;
    }

    public int CompareTo(XactTask rhs )
    {
        //EXCEPTION OCCURS HERE AFTER A NULL RHS HAS BEEN PASSED IN**
        Console.WriteLine("comparing " + rhs.priority+"to "+this.priority); 

        if (rhs.priority <= this.priority)
        {
            Console.WriteLine("Entered first if");
            Console.WriteLine("comparing " + rhs.submitted + "to " + 
                this.submitted);
            if (rhs.submitted <= this.submitted)
            {
                Console.WriteLine("Entered second if");
                return 1;
            }
            else
            {
                return -1;
            }
        }
        else
        {
            Console.WriteLine("In else");
            return -1;
        }
    }
}
}

And the class to hold the list is defined like this:

namespace PrioritisedRequestQueue
{
    public class Class1
    {
        public List<XactTask> tasks;

        public Class1()
        {
            tasks = new List<XactTask>();
        }

        public void sort()
        {
            tasks.Sort();
        }

        public void add(XactTask task)
        {
            tasks.Add(task);

        }

    }
}

Here is the program I use to do the testing:

PrioritisedRequestQueue.Class1 tester = new PrioritisedRequestQueue.Class1();

tester.add(new PrioritisedRequestQueue.XactTask(1, DateTime.Now, "test1"));
tester.add(new PrioritisedRequestQueue.XactTask(1, DateTime.Now, "test2"));
Console.WriteLine(tester.tasks[0].name);
Console.WriteLine(tester.tasks[1].name);
tester.tasks.Sort();

The output of the test program is:

 comparing 1to 1 Entered first if
 comparing 16/05/2011 14:41:57to 16/05/2011 14:41:57 
 Entered second if
 comparing 1to 1 Entered first if
 comparing 16/05/2011 14:41:57to 16/05/2011 14:41:57 
 Entered second if
 comparing 1to 1 
 Entered first if
 comparing 16/05/2011 14:41:57to 16/05/2011 14:41:57 
 Entered second if
 comparing 1to 1 
 Entered first if
 comparing 16/05/2011 14:41:57to 16/05/2011 14:41:57 
 Entered second if

Then it seems to pass a null rhs object into the compare method and I get a NullReferenceException when it tries to access the priority of rhs.

Can you tell me why this is happening and what I have to do to address this?

Please let me know if you need any more information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:30:17+00:00Added an answer on May 22, 2026 at 12:30 pm

    When I run your code, I get an InvalidOperationException because CompareTo is not implemented correctly.

    Your CompareTo implementation must return 0 if rhs is the same as this (i.e., this object and the object being compared are equal). It also should not fail if rhs == null, but should order null in a consistent way; this is typically done by always returning 1 to sort null before all valid objects.

    The documentation for IComparable.CompareTo lists the following rules, which must be followed in order for Sort to function correctly:

    • A.CompareTo(A) is required to return
      zero.

    • If A.CompareTo(B) returns zero, then
      B.CompareTo(A) is required to return
      zero.

    • If A.CompareTo(B) returns zero and
      B.CompareTo(C) returns zero, then
      A.CompareTo(C) is required to return
      zero.

    • If A.CompareTo(B) returns a value
      other than zero, then B.CompareTo(A)
      is required to return a value of the
      opposite sign.

    • If A.CompareTo(B) returns a value x
      that is not equal to zero, and
      B.CompareTo(C) returns a value y of
      the same sign as x, then
      A.CompareTo(C) is required to return
      a value of the same sign as x and y.

    To fix the problem, ensure that your implementation of CompareTo returns 0 when priority == rhs.priority && submitted == rhs.submitted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement some STL-style sorting algorithms. The prototype for std::sort looks something
I'm trying to implement a sorted list as a simple exercise in Java. To
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
We are trying to implement a REST API for an application we have now.
In a game, I'm trying to keep a list of users and have it
I am trying to sort my ArrayList, but when I check the first item
Trying to implement the following behavior on an iPad. I have a map-centric application
Hi: I'm trying to sort a list of tuples in a custom way: For
I'm trying to put some key values in HashMap and then trying to sort
All I am currently trying implement something along the lines of dim l_stuff as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.