How to create dictionary array for list items

Hi @UiPath_Community,

I need to create a dictionary array for the list items fetched using for each list item as shown below. I am getting the list item index and then passing it with dictionary to create an array but when I run it throws me error at assign activity - ‘Object reference not set to an instance of object’. Thanks to @Yoichi for below post, I am trying to assign dictionary values like mentioned in below post:

Regards
Sonali

1 Like

Note that a dictionary is not an array, it is very different from an array.

Go to your variables pane and in the default for your dictionary variable put:

New Dictionary(Of String, Object)

I’m just assuming String, Object because that’s what is typically used. You may have other requirements, a dictionary can be many things ie String,String, String,Datatable, or even a dictionary of dictionaries (Of String, Dictionary(Of String,Object)

1 Like

Hi @postwick,

I have created a variable of type dict(string,object)

My bad, I should have mentioned array of dictionaries.

Regards
Sonali

1 Like

You have to initialize it, not just create it. Again, put:

New Dictionary(Of String, Object)

…into the variable’s Default. This initializes it. If you don’t do that, it’s null, which generates the object reference error.

1 Like

can you share a screenshot of your variable panel

it should like this ,

1 Like

Hi @postwick @Achiranga,

Variable panel as below:
image

Please note, I have another dictionary(string,object) created as well, and its working fine, there is no need to initialize.

This error is only coming when I am trying to assign that dictionary to a dictionary array(shown in variable panel above).

Regards
Sonali

1 Like

okay.

I was able to get past the error by doing below:

image

{new dictionary(Of String,Object),new dictionary(Of String,Object)}

However, now the problem is any number of dictionaries can come in this array based on the filter value. So, I can’t initialize this array that many times in advance.

Is there a way I can assign all dictionaries at once to this array?
Or may be keep joining dictionaries to the array?

Regards
Sonali

1 Like

Before adding the new value to the array, you have to initialize the new array member as New Dictionary(of String,Object)

Show me your code where you’re adding the dictionary to the array.

1 Like

Its as shown below:

dict(getListIndex) = ListItem.Fields.ToDictionary(Function(x) x.Name, Function(x) x.Value)

Code works fine till the level I have initialized array in variable panel but based on the filter, no of items can change, so it needs to be initialized at runtime or some other way…

1 Like

Then before that statement do

Assign dict(getListIndex) = New Dictionary(Of String, Object)

This will initialize the dictionary at the array location.

1 Like

@postwick,

Already tried that, throws error in that statement itself if i add that.

Regards
Sonali

1 Like

What error is thrown in that statement?

1 Like

This worked fine for me.

image


Result:

1 Like

Did you create your list as Array(of Dictionary,Object)? Arrays are static, you can’t change the number of elements after they’re created. That’s why I use IEnumerable(of …) and then initialize it as “New List(of…)” - that way you can use the .concat({newelement}) expression to add items to it.

1 Like

@postwick,

No, I am using Array of dictionary, that explains why it didn’t work for me.

Thing is I have a process which uses get list items - Sharepoint package output of which was dictionary array, but that is no longer working, hence switching to get list items -Office 365 api which gives output as datatable.

Intention is to be able to get the data in same format(dictionary array, as it was before) so we don’t have to change the further underlying code.

That’s why trying to stick to dictionary array.

Regards
Sonali

1 Like

You don’t have to stick to dictionary array. They’re all basically interchangeable. Try it the way I showed you with the variable datatype as IEnumerable(of dictionary(of string,object)) and use the .concat operator to add items.

1 Like

I followed another approach(used for each list item) to navigate through list items instead of saving those in the dictionary array.

Challenge was to dynamically initialize dictionary array as number of list items can vary.

If you use IEnumerable instead of Array, and initialize it as New List(Of datatype) then you don’t have to initialize it with a specific number of elements. You can just add elements to it with .concat

1 Like

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