1

I have a SharePoint list that I'm reading. In this list there are a number of items that don't have a value assigned to them, throwing an error. The code I'm using is the following:

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;
        }
    }

The following line being the problem:

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

If I change that line to not try to read the assigned to field, like the following, it works fine:

select LoadItems(   "Unassigned",

All I can deduce from this is that the null collase operator I'm using here to evaluate if the assigned to field is blank or not isn't working as I'm expecting it to, but I can't understand why. How am I meant to be thinking about this?

Michael A
  • 9,480
  • 22
  • 70
  • 114
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Feb 25 '15 at 04:42

1 Answers1

2

Regenerating issue,

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

Reason for exception is NOT collase operator (??). But the use of ToString() with a null.

It can be confirmed with following example,

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

Or more clearly here,

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

Hope this solved your confusion.

Furthermore here's how you can solve it :

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

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

So your code :

 select LoadItems(  (item["Assigned To"] ?? "Unassigned").ToString() ....
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46