Exception when referencing non-nullable field and even TryGetValue on various DataContainers

2024/12/13 7:33 PM

Was trying to retrieve from the IWebPageContentQueryDataContainer's WebPageItemParentID (int), however it throws an exception if the content item has no parent (does not default to 0, the type is int not int?).

Likewise, the TryGetValue on that columns ALSO throws an exception, vs. returning "false" as you would expect from a try catch error.


Environment

  • Xperience by Kentico version: [30.0.0]
  • .NET version: [8]

Answers

2024/12/13 7:35 PM

As much as it's an Anti-pattern, a temporary solution until this issue is resolved is to use the TryGetValue, BUT make your output variable a nullable as well. As shown in Sean's [Community Portal](community-portal/src/Kentico.Community.Portal.Web/Features/SEO/SitemapRetriever.cs at v29.7.0.4 ยท Kentico/community-portal)

                    int parentId = 0;
                    if(x.TryGetValue(nameof(WebPageFields.WebPageItemParentID), out int? parentIdVal) && parentIdVal.HasValue) {
                        parentId = parentIdVal.Value;
                    }
2024/12/18 8:14 PM
Answer

Final Update, Support is labeling this as a clear bug, and the WebPageItemParentID will fall back to 0 if it's null. Additionally they are looking at the TryGetValue behavior to handle nulls and return false.

To answer this question, you have to login first.