NullReferenceException 我似乎无法用 Null Collase 运算符解决

NullReferenceException I can't seem to solve with Null Collase operator

我有一个正在阅读的 SharePoint 列表。在此列表中有许多项目没有分配给它们的值,从而引发错误。我使用的代码如下:

public class FacilitiesDal : BaseDal
{
    public List<FacilitiesEntity> FetchItems(string siteName, string listUrl)
    {
        try
        {
            using (var site = new SPSite(siteName))
            {
                using (var web = site.OpenWeb())
                {
                    PostEvent("Successfully opened: " + web.Url, BaseExceptionEventArgs.ExceptionLevel.Debug);
                    PostEvent("Attempting to load list: " + listUrl, BaseExceptionEventArgs.ExceptionLevel.Debug);

                    return (from SPListItem item in web.GetList(listUrl).Items
                            select LoadItems(   item["Assigned To"].ToString() ?? "Unassigned",
                                                item["Site(s)"].ToString(),
                                                item["Job Category"].ToString(),
                                                item["Status"].ToString(),
                                                Convert.ToDateTime(item["Date required?"]),
                                                item.ID.ToString(),
                                                item.ContentType.Name,
                                                item.DisplayName,
                                                item.Name,
                                                "",
                                                item.Url,
                                                item["Created By"].ToString(),
                                                item["Modified By"].ToString(),
                                                Convert.ToDateTime(item["Created"]),
                                                item["Created By"].ToString(),
                                                Convert.ToDateTime(item["Created"]),
                                                item["Created By"].ToString())).ToList();
                }
            }
        }
        catch (Exception ex)
        {
            PostEvent("Error fetching Facility list items", BaseExceptionEventArgs.ExceptionLevel.Error, ex);

            throw;
        }
    }

下一行是问题所在:

select LoadItems(   item["Assigned To"].ToString() ?? "Unassigned",

如果我将该行更改为不尝试读取分配给字段,如下所示,它工作正常:

select LoadItems(   "Unassigned",

我可以从中推断出我在这里使用的 null collase 运算符来评估分配给字段是否为空没有像我期望的那样工作,但我不能明白为什么。我怎么会想到这个?

再生问题,

  string x = null;    
  String res = x .ToString() ?? "Unassigned";    // <= Will throw NullReference excep.
  Console.WriteLine(res +" "+ x.ToString());

异常原因是NOT 拼接运算符 (??)。而是用ToString()加上一个null

可以用下面的例子来确认,

 String x=null;
 Console.WriteLine(x.ToString()); //<= Will throw NullReference excep.

或者更清楚这里,

 Console.WriteLine(null.ToString()); //<=  Operator '.' cannot be applied to operand of type <null>

希望这能解决您的困惑。

此外,您还可以通过以下方式解决它:

  String x="test";
  Console.WriteLine((x ?? "notset").ToString()); // <= Will out test

  String y=null;
  Console.WriteLine((y ?? "notset").ToString()); // <= Will out notset

所以你的代码:

 select LoadItems(  (item["Assigned To"] ?? "Unassigned").ToString() ....