简体   繁体   English

访问字段值时检查Null值

[英]Checking for Null values when accessing field values

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. 假设该项目是有效的Outlook ContactItem,我认为以下代码将检查null值,如果相关字段为null,则返回“(Empty)”。

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. 从严格意义上说,该错误不是NullReferenceException-该错误与联系人字段有关,而不与ContactItem对象有关。

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 ? 现在,我会说对了,错误实际上是在告诉我该字段实际上是一个对象,如果该对象确实包含文本,则会从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. 我试图通过填充Outlook联系人中的每个字段来减轻该错误-不会引发该错误-但是如果我删除字段内容(例如CompanyName),则该代码将在该行上失败,并显示相同的错误。

If I am right, then how would I check if the object exists prior to trying to get the field contents? 如果我是对的,那么在尝试获取字段内容之前,我将如何检查对象是否存在?

MTIA MTIA

DWE DWE

Try using the null-conditional operator. 尝试使用空条件运算符。

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

Example: If Property is null: 示例:如果Property为null:

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

If item is null: 如果item为null:

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

Or check both 或同时检查

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

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 数据类型为EntryID

public string? EntryID;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM