Avoid NullReferenceException when casting null to value type