Skip to content

[Bug]: EasyPostObject.GetHashCode() throws null error when Id is null #678

Closed

Description

@TManITtech

Software Version

7.12.0.0

Language Version

.NET Framework 4.8

Operating System

Windows 11

What happened?

When it [automatically] compares 2 child class objects of EasyPostObject (that are created manually and the Id wasn't set yet) it throws System.NullReferenceException: Object reference not set to an instance of an object.

What was expected?

The code should check first if the Id is null:

    public override int GetHashCode()
    {
        return base.GetHashCode() ^ (Id ?? "").GetHashCode();
    }

Sample Code

List<Shipment> ShipmentList = new List<Shipment>();

// Replace the (not yet purchased) copies in ShipmentList with the purchased boxes.
foreach (var box in CurrentOrder.Shipments)
{
	var sameFound = ShipmentList.FirstOrDefault(sh => string.IsNullOrEmpty(sh.TrackingCode)
													  && sh.Parcel?.Weight == box.Parcel?.Weight
													  && sh.CarrierAccounts?.Any(c => c.Readable?.ToUpper() == box.SelectedRate?.Carrier?.ToUpper()) == true);

	if (sameFound != null)
	{
		//EasyPostExt.MakeSureItHasAnId(sameFound); // Prevents the error.
		ShipmentList.Remove(sameFound); // Error happens here!
	}

	ShipmentList.Add(box);
}

Relevant logs

System.NullReferenceException: Object reference not set to an instance of an object.
   at EasyPost._base.EasyPostObject.GetHashCode()
   at EasyPost._base.EasyPostObject.Equals(Object obj)
   at System.Collections.Generic.ObjectEqualityComparer`1.IndexOf(T[] array, T value, Int32 startIndex, Int32 count)
   at System.Collections.Generic.List`1.IndexOf(T item)
   at System.Collections.Generic.List`1.Remove(T item)
   at EasyPostShipping.ShipmentOrder.Buy(Rate rate) in C:\Users\user\source\repos\EasyPost\EasyPostShipping\Model\ShipmentOrder.cs:line 798
   at EasyPostShipping.EasyPostShippingController.<Ship>d__191.MoveNext() in C:\Users\user\source\repos\EasyPost\EasyPostShipping\Controller\EasyPostShippingController.cs:line 2520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions