Friday 14 February 2014

"Object reference not set to an instance of an object." - working with SharPoint 2010 fields

You can use this code to check values:

                        foreach (SPListItem row in list.Items)
                        {
                            //Checks if "Number" field contain any data
                             if (row["Number"] != null)
                                Console.WriteLine("Value of field is: {0}", row[myLookupColumnName].ToString());
                        }


or another way how to treat empty field is to use try and catch block like this:

                        foreach (SPListItem row in list.Items)
                        {
                            //Tries to catch exception
                            try
                            {
                                Console.WriteLine("Value of field is: {0}", row[myLookupColumnName].ToString());
                            }
                            catch (NullReferenceException ex)
                            {
                                Console.WriteLine("The field was empty. Error: " + ex.Message);
                            }
                        }

No comments:

Post a Comment