0

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

DWE
  • 77
  • 9
  • 2
    If `item` is null, accessing `item.EntryID` will cause this exception. If `EntryID` is null calling `item.EntryID.ToString()` will throw this exception, and so on, and so forth. Sichi's suggestion is good. – Llama Sep 30 '18 at 09:02
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Llama Sep 30 '18 at 09:02

2 Answers2

6

Try using the null-conditional operator.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators

Example: If Property is null:

EntryID = item.EntryID?.ToString() ?? "(nothing)";

If item is null:

EntryID = item?.EntryID.ToString() ?? "(nothing)";

Or check both

EntryID = item?.EntryID?.ToString() ?? "(nothing)";
Sichi
  • 267
  • 2
  • 10
  • Hi Sichi, I didn't realise that you could coalesce as you have shown. Thank you. – DWE Sep 30 '18 at 09:21
  • 1
    If item is null, you can't use your second option. You must use the third (checking both). Once anything in the call chain can be null, you need to use the null propagation operator on each successive property – pinkfloydx33 Sep 30 '18 at 09:51
-2

Try Using Ternary Conditional Operator

Condition ? True : False

If you are retriving data from Database then this

if(item.EntryID.ToString() == DBNull) ? item.EntryID.ToString() : "(nothing)";

And if still shows error then try to put ? with datatype of EntryID

public string? EntryID;
Sheraz Iqbal
  • 50
  • 1
  • 4
  • `public string? EntryID;` will show an error. Nullable reference types won't be with us until C# 8. And i'm also fairly certain that calling `item.EntryID.ToString() == DBNull` will fail when `item` is null or `item.EntryID` is null. – Llama Sep 30 '18 at 09:25