.Contains function throwing error

I have a conditional statement saying “String.IsNullOrEmpty(message) or message.Contains(”!“)” but the portion checking for an ! is throwing a “Object reference not set to the instance of an object” error. When I take out that portion, it runs fine. Any ideas? Thanks

1 Like

Check if your variable message has a valid value. Could you print with Write Line your “message” variable before evaluete it?

@jpreziuso
The If activity does not use lazy boolean checking which means that it will always evaluate every condition you give it. If message is null or empty then the message.Contains function will always throw “Object reference not set to the instance of an object.” because Studio checks that when it gets to the If.

1 Like

The variable is empty…it is supposed to be. Would this cause an issue when also checking to see if it contains something?

This makes sense. Is there a simple fix to this? I tried two separate “If” activities but that didn’t seem to work. I also tried just checking to see if it contains a ! and that throw the same error. So it seems as though you cannot use the contains function for an empty string. I think I will have to put that portion in the “Else” clause

1 Like

@jpreziuso
The Contains function works on an empty string but it doesn’t work on a null string.
Putting the second if inside the else of the first should fix this error.

1 Like

Hi @jpreziuso
Buddy, actually your approach of if condition is perfect…but the thing is we used two conditions that doesn’t satisfy the null value in message variable rather we need one at a time to be applied…
Yes, if the variable message has no value, this part of if condition will work
““String.IsNullOrEmpty(message)”, because it validates whether it is null or not and it accepts even null value, so this error “Object reference not set to the instance of an object” will not be thrown…
But
as we have another condition like this message.contains(“!”) next to our previous condition, bot will got to this condition as well for validation, and as this condition will check for ! only when the message contains some value in it, so it wont accept NULL values in it,and thus throwing error like this “Object reference not set to the instance of an object”

So to avoid this we need to do like this actually
First we need to mention this condition alone in the if condition,String.IsNullOrEmpty(message)
and if this condition gets passed it will go to THEN part of if condition where we can mention simply without any condition or any activity while if this condition gets failed it will go to ELSE part where we can include this condition with another if condition in the ELSE part
message.Contains(”!"),

this will work for sure buddy
Cheers @jpreziuso

1 Like

Yes I just realized this myself and have it working now. Thanks

Fantastic
Cheers @jpreziuso

You can use lazy evaluation so if the first expression returns true it will not check the second expression. Just change the ‘Or’ in your statement to ‘OrElse’ so it would be:

“String.IsNullOrEmpty(message) OrElse message.Contains(”!“)”

3 Likes

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