0

Here is my code I am new to programming: I am trying since morning but couldn't figure out the solution, please correct my code and help me with it. Thank you.

other.ClientData.NumDay <== getting null here.

I am getting this error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

assignment3.Booker.ClientData.get returns null.

public int CompareTo(Booker other)
{
        if (this.ClientData.NumDay > other.ClientData.NumDay)
        {
            return 1;
        }
        else if (this.ClientData.NumDay < other.ClientData.NumDay)
        {
            return -1;
        }
        else
        {
            return 0;
        }
}


List<Booker> appointment = new List<Booker>(8)

foreach (var val in appointment)
{
    Console.WriteLine(val);
}
appointment.Sort();

foreach (var val in appointment)
{
    Console.WriteLine(val);
}

1 Answers1

0

Please check what is actually coming in other here.

public int CompareTo(Booker other)
{
    console.log(other, other.ClientData)
}

There can be ClientData spelling is different or other does not have ClientData or other can be null too. Please check and rectify

Also, you can keep a check as

public int CompareTo(Booker other)
{
    if(other && other.ClientData)
    {
        if (this.ClientData.NumDay > other.ClientData.NumDay)
        {
            return 1;
        }else if (this.ClientData.NumDay < other.ClientData.NumDay)
        {
            return -1;
        }
        else
        {
            return 0;
        }
    }
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Trouble
  • 90
  • 6