0

I have this line of code in a method I'm writing:

 Dictionary<string, object> fieldsVal = file.ListItemAllFields.FieldValues;
 if (fieldsVal.ContainsKey(System.Xml.XmlConvert.EncodeName("State0")) && fieldsVal[System.Xml.XmlConvert.EncodeName("State0")].ToString() == "12 final approvement" && (extension == 
".csv" || extension == ".jpg" || extension == ".png" || extension == "xlsx"))

I get a System.NullReferenceException in the line where the if statement starts and I don't understand why.

1 Answer 1

0

Perhaps the fieldsVal[System.Xml.XmlConvert.EncodeName("State0")] contains an empty (null) value and then it can not convert it to a string.

Try to always have a valid string without checking for null:

Dictionary<string, object> fieldsVal = file.ListItemAllFields.FieldValues;
 if (fieldsVal.ContainsKey(System.Xml.XmlConvert.EncodeName("State0")) && (fieldsVal[System.Xml.XmlConvert.EncodeName("State0")] + "") == "12 final approvement" && (extension == 
".csv" || extension == ".jpg" || extension == ".png" || extension == "xlsx"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.