For Each Loop : Object reference not set to an instance of an object

Hi, I get the below error when I am trying to read excel files one by one using for each loop and write to queue using reframework. I am invoking WriteDataToQueue.xaml from GetTransactionData.xaml.

For Each: Object reference not set to an instance of an object.

I have declared in-config as an argument in the WriteDataToQueue.xaml of the type Dictionary object. Could someone advise what could be causing this.

have a check if the in_config argument of WriteDataToQueue.xaml is set by invoke from GetTransactionData.xaml

Debug the flow with breakpoints / step in/out… and inspect the locals

yes, I have set the argument in invoke from GetTransactionData.xaml

1 Like

what is all included in your “in” portion of the for each loop? It is cut off in your screenshot

Object reference not set to an instance of an object generally means you are trying to reference a null value or an object that is null. Verify your string key is correct (i believe it is case sensitive) and verify that the value in excel is exactly what you expect. I’m guessing it has to do with the object you are looping over that is being cut-off in the screenshot though


Thank you for responding. Below is what I have defined in the For loop.
the key is defined in the config sheet.

Name Value
InputDataPath D:*\Data\Input\CustomerStockDetails

It is failing before it does any looping. The circled part is cut off. Can you show the full text from this circled part?


EDIT: Also, re-reading your comment, the InputDataPath in your config is just a single string. What exactly are you expecting to loop over in your for each loop?

PFB the full text


I am trying to loop data from excel sheets → Read a file’s contents, write to queue, process some steps & move to the next file. In short, this is what I am trying to achieve.
InputDataPath → I have defined the path from which excel files should be picked. I am trying to loop excels from this path

Ok, to do that you need to tell uipath that you want to pick up all excel files from that path. Right now it is just saying for each object in string (which doesn’t make any sense).

Change the TypeArgument in the for each property to be String if it isn’t already. Then change the for each loop so it is iterating over Directory.GetFiles(in_config("InputDataPath").ToString,"*.xlsx")

The Directory.GetFiles method returns an array of string, where each string in the array contains the full file path for each file in that directory. I am filtering it so it only picks up files ending in .xlsx, but you can remove that filter entirely or change the extension (e.g. change to .xls to include .xlsm, .xls, .xlsb, etc files in your array). Then your for each loop will take one string at a time to process

EDIT: You can also change the SearchOption in your Directory.GetFiles method, which would allow you to search all subdirectories. If you want to do that, just alter it so it includes… "*.xlsx",SearchOption.AllDirectories) at the end

EDIT #2: You’ll also need to change your excel scope so it just says item instead of "Data\Input\CustomerStockDetails\" + item. And since you changed the TypeArgument of the for each loop earlier, you can just have it be item instead of item.ToString since item is already a string

1 Like

Thank you for the response @Dave

I updated the path taht is read as below. But even then I get the same error.

Hi @anon24920977, what Error is thrown ?

@NIVED_NAMBIAR It is same as the title. Object reference not set to an instance of an object. Below is a screenshot.

Hello @anon24920977

Try again after changing the direction of In_Config argument from In to In/Out in you Get TransactionData.xaml

I would recommend to always use In/Out direction for your in_Config dictionary in all the xamls.

That did not help either. Attaching my workflow, if you can have a look.
I am calling Dispatcher_for_Queue.xaml from InitAllSettings.xaml. (1.1 MB)

Check whether files are present in folders @anon24920977

@NIVED_NAMBIAR yes they are.


Could someone assist me here please. Appreciate your help.

Try the following:

  1. As per Dave’s comment, change the ArgumentType to String (it is still assigned to object in the workflow you attached):

  1. Assign your dictionary out_Config argument when you invoke the Dispatcher workflow as this was blank and wasn’t feeding in your in_config key:value pairs :


Thanks for responding @byuli

I had tried with typeargument as both string and object but neither worked. I have updated the argument assignment while invoking the file as well.
But the issue is not resolved. (1.1 MB)

Are you getting the exact same error in the same place? I have just tried your workflow and it worked without any issues. Not sure if restarting the robot service/machine would help here.

Could you please validate that you don’t have ‘in_config’ listed as a variable? If you have both an argument and a variable with the same name, it will always take the value of the variable, since it goes with the most local scope possible