Foreach error in document understanding

I am getting files from a folder using “Directory.GetFiles(path)” in foreach.

image

picking invoices one by one and process them in document understanding to read required data. it is working fine for one file. but when processing multiple invoices then it is processing one less from total count, I mean if 5 files in folder then process 4 successfully and getting error for last.

“Object reference not set to an instance of an object.”
image

Please help me.

Can you show us more of the workflow? What is going on inside the For Each body?

I have changed the foreach to while loop to fix the problem. its working now but not worked with foreach.

can you tell me how you solved using while loop ? I am facing similar issue!!!