If: Object reference not set to an instance of an object. is null or empty

hello all,
There is few post how to use isNullorempty but never managed to work

Im getting Json object , loop though it , but when i have null value or missing properties
i got this msg “If: Object reference not set to an instance of an object” which is normal,
but couldnt figure out how to skip it (i have a flag need to change the value of its null or empty)

any tips


HI @Hazem_Saleh

Check for the response being received.
Json objects have children in in, so you can check for its count.
If jobj.count>0
then proceed with steps
log message, no data found.

Happy Automation!

Hi @Hazem_Saleh,

Basically what you need to do is, Check for specific keys if they exist or not in the JObject, if the key does exist, basis that you need to consider whether is coming with isNullOrEmpty to determine blank/no values for the key.

keyExists = jObjectVariable.ContainsKey(“yourKey”)

Please note keyExists is of type Boolean

Not sure the exact syntax for a JSON object, but for a dictionary here is how you do it. In your IF condition you check if the key exists, and use the AndAlso operator to then check if it’s null:

myDict.ContainsKey(“someKey”) AndAlso String.IsNullOrEmpty(myDict(“someKey”).ToString)

The AndAlso operator will ONLY process the part after it if the part before it is true. So if the someKey key doesn’t exist, (ContainsKey returns false) then the IsNullOrEmpty won’t throw the object reference error.

Thanks but count will not work if its no reference :confused:

Contain keys work like a charm Thanks Jatin

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.