String.Length is throwing object reference not set to an instance of the object error
I have a string variable which is null.
Since it’s null it’s throwing the object reference not set to an instance of the object error. But how can I get the value as “0” if the string value is null.
Eg: nameStr = “”
nameStr.Length — this is throwing the error
In C#, if a string is null, attempting to reference a method or property such as .Length on it will throw a NullReferenceException (the “object reference not set to an instance of an object” error), because null means “does not exist”, so there’s nothing on which to execute a method or property.
However, there is a way to avoid this exception:
Add an Assign activity for a System.Int32 variable with value If(nameStr IsNot Nothing, nameStr.Length, 0)
If the value is not Nothing, it will display the length of the string, otherwise it will display 0.
You are saying “nameStr IsNot Nothing”, how is that possible? It should be “nameStr Is Nothing” right? What am I missing here? But your statement worked.