How to make it so that if the attempt to retrieve data from the specified tag in xml files fails, instead of an error "Object reference not set to an instance of an object "was an empty value recorded? For example, there is an xml file “test” in which the Area tag is missing, an error is required “Object reference not set to an instance of an object” did not terminate the program, but simply wrote an empty value. How to do this?NamespaceKZVUandKVOKSXPath3.zip (82.2 KB) test.xml (11.2 KB)
This exception will occur if a variable is not initialised (is null).
Run your workflow in debug and add a breakpoint to the activity that is breaking, and look at the variable values in the locals panel. You should see a variable used that is null. Take a look at the activity giving the variable it’s value, and see why.
Using the if condition, I check if the variable is empty.
How to make it so that if empty, then write ‘’ null 'or space to the table?
If not empty, write down the value.
What variable type is
areaKVOKS = "" in the if statement
If the tag is absent, then assign an empty value to the areaKVOKS variable and write it through add data row, if not empty, then write the extracted value. How to do it?
You would want a log message or message box on the
Then side of the if statement to show the value of areaKVOKS. You may need to specify .toString after it but I’m not sure.
On the right, you would have your assign as shown in the screenshot you’ve attached.
Below the if statement goes your add data row, to add the areaKVOKS variable to a datatable. I’m not sure about your datatable structure so I will leave that up to you to do.
Forgive me, remove the variable from the log message. It wouldn’t work because the variable is null and we had already established that.
But now there is an error in add data row (“Object reference not set to an instance of an object”.) as we are trying to add a tag that is not there. How do I set areaKVOKS (data type XElement) to an empty value?
areaKVOKS is already an empty value, hence why you’re getting your exception message. You can’t output the variable, so in your log message you need to put this:
"Variable areaKVOKS is an empty value"
Or something similar
Add data row process with Try Catch?
You could try that, and hn the Catch section you could add a log message to say what the exception was. This would just be the variable
have a check on a different approach:
lets keep the returned Null to make clear, that the element was not found
for a defensive access use following:
left side: yourValueReceiveVar
if(isNothing(yourXElementVar), “ValueNotFound”, yourXElementVar.Value.toString)
The more important question to clear is about:
- was it not found as it exists not OR due an bug in the retreival statement
That is how it should be?
The data type of areaReceive String.
As before, there was an error in the Add data row.
try it. Waht the result?
what is the answer to the from quote: can it be the case taht the element is missed in some cases or not?
In some xml files the Area tag is missing. Empty values in the tag reads normally.
can you share your xaml, so I can do some checks? thanks
Main.xaml (41.2 KB)