IF - Object reference not set to an instance of an object

Would appreciate your help on the following:

I have an IF statement with the following condition:
out_argument.trim.Contains("-")

I have also tried to check if the string is empty as the condition:
NOT string.IsNullOrEmpty(out_argument) AND out_argument.trim.Contains("-")

The type of out_argument is a string but every time it comes empty since it couldn’t read the specific value, it throws the following error right before entering the IF statement with the conditions mentioned above:

Object reference not set to an instance of an object.

On the THEN side of the IF statement, if out_argument contains “-”, it will proceed to do additional operations, the ELSE side is blank.

Any thoughts on this?

Thanks!

This is a good way to check it, but you should change And to AndAlso, since And doesn’t short circuit in VB.NET: https://blog.codefront.net/2004/01/25/vbnet-boolean-operators-and-short-circuit-evaluation

1 Like

@rjackson

As you are passing a argument, check you are passing the argument correctly

Also check any variable is created with the same name also

Object Reference error is mainly focused that the variable is having null value

Check by debug

Hope this helps you

Follow Link

Thanks

First check string is not nothing. If string is not nothing keep nested if in this to check string contains “-” or not.

If out_argument isnot nothing
if out_argument.trim.contains("-")
Take Necessary Action
else
Take actions argument is containing “-”
else
Argument is not assigned any value.

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