Skip to content

Critical: NullReferenceException in MarketPlace.GetMarketPlaceByID due to static initialization orderΒ #907

@pandapknaepel

Description

@pandapknaepel

I am encountering a NullReferenceException when calling MarketPlace.GetMarketPlaceByID(string id).

Cause:
The issue is caused by the static initialization order within the MarketPlace class. The static list _allMarketplaces is declared and initialized at the top of the class, before the individual static marketplace fields (like US, Canada, etc.) are defined.

Because C# initializes static fields in the order they appear in the source code, _allMarketplaces is populated with null values. When GetMarketPlaceByID is called, the LINQ FirstOrDefault call attempts to access .ID on a null object, leading to the crash:

at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
at FikaAmazonAPI.Utils.MarketPlace.GetMarketPlaceByID(String id)

Proposed Fix:
The _allMarketplaces list must be moved below the individual marketplace definitions to ensure they are instantiated before being added to the list.

Note: A PR (#906) has already been submitted by @babuscam to address this specific issue.

@abuzuhri, could you please review and merge this fix as it currently breaks the utility methods in the latest version?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions