I am attempting to check a field value for null.
Assuming that item is a valid Outlook ContactItem, the following code, I would have thought, would have checked for a null value and returned "(Empty)" if the relevant field is null.
EntryID = item.EntryID.ToString() ?? "(nothing)";
Title = item.Title.ToString() ?? "(nothing)";
First_Name = item.FirstName.ToString() ?? "(nothing)";
Middle_Name = item.MiddleName.ToString() ?? "(nothing)";
Last_Name = item.LastName.ToString() ?? "(nothing)";
Suffix = item.Suffix.ToString() ?? "(nothing)";
Company = item.CompanyName.ToString() ?? "(nothing)";
Home_Phone = item.HomeTelephoneNumber.ToString() ?? "(nothing)";
Mobile_Phone = item.MobileTelephoneNumber.ToString() ?? "(nothing)";
FirstLastName= item.LastNameAndFirstName.ToString() ?? "(nothing)";
However, what is happening on other fields, is an error as follows:
Object reference not set to an instance of an object.
The error is not a NullReferenceException in the strict sense - the error relates to the contacts field not the ContactItem object.
Now would I be right in saying that the error is actually telling me that the field is in fact an object, which if it does contain text, is removed from the ContactItem ?
I have attempted to mitigate the error, by filling in each field in an Outlook Contact - the error does not get thrown - but if I delete the field contents, of say CompanyName, the code will fail on that line with the same error.
If I am right, then how would I check if the object exists prior to trying to get the field contents?
MTIA
DWE