C# 10 Nullable Reference Types: Unexpected NullReferenceException When Accessing Properties
I've tried everything I can think of but I'm following best practices but I'm relatively new to this, so bear with me. I'm working with a `NullReferenceException` in C# 10 when trying to access properties of a class that has been configured with nullable reference types. I have an `Order` class that contains a `Customer` object, which can be null under certain conditions. I expected to handle this scenario gracefully, but I'm still getting exceptions when I access `Customer.Name` without checking for null. Hereβs a simplified version of my code: ```csharp public class Customer { public string? Name { get; set; } } public class Order { public Customer? Customer { get; set; } } public void PrintCustomerName(Order order) { // This line throws a NullReferenceException if Customer is null Console.WriteLine(order.Customer.Name); } ``` I have tried adding a null check before accessing the `Name` property, like this: ```csharp if (order.Customer != null) { Console.WriteLine(order.Customer.Name); } else { Console.WriteLine("Customer is null"); } ``` However, the scenario still continues when I call the `PrintCustomerName` method with an `Order` that has a null `Customer`. I also ensured that nullable reference types are enabled in my project settings. Am I missing something in how I should be handling nullable properties? Any insight on what could be leading to this unexpected behavior would be greatly appreciated. I'm working on a API that needs to handle this. Any help would be greatly appreciated! Is there a better approach? This is part of a larger REST API I'm building. Any pointers in the right direction? My team is using C# for this web app. Is there a better approach?