Twitter does not provide user Birthdays through its REST API. Because of that, I prepared a nullable DateTime property.
I also prepared an Interface for all social network services. Most of them are async Task methods.
But I can't get to pass this error: Object reference not set to an instance of an object even though I'm not using the object.
The Interface method GetUserBirthDayAsync is implemented as follow for Twitter service:
public Task<DateTime?> GetUserBirthDayAsync(){
return null; // because twitter does not provide user birthday
}
and this is how I used the method:
DateTime? BirthDate = await socialNetworkService.GetUserBirthDayAsync();
The error occurred in setting the value to the BirthDate variable.
What is happening? What is causing the exception?
Try this:
public Task<DateTime?> GetUserBirthDayAsync()
{
return Task.Run(() => (DateTime?)null);
}
Or, better way to do the same:
public Task<DateTime?> GetUserBirthDayAsync()
{
return Task.FromResult<DateTime?>(null);
}
When you just return null
that means, your Task is null
, not result of async method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With