Why I get System.NullReferenceException
running a code like:
var Response = new Response()
{
Id = request?.Event.Id
};
Shouldn't I just get Null value for Id property (Id is a string)?
Why I get System.NullReferenceException
running a code like:
var Response = new Response()
{
Id = request?.Event.Id
};
Shouldn't I just get Null value for Id property (Id is a string)?
Your current code only take into account if request
is null
and Event
will not be returned if that was the case. If Event
is null
in the request
instance then that would still lead to an NRE when you try to access the Id
property.
The fix is to also add the null conditional operator to Event
which will return null
if Event
is null and not try to access Id
.
request?.Event?.Id