Clear data table - Object reference not set to an instance of an object

I have for each folder loop where trying to add data from csv file into data table finalnaTabela.

After first iteration data is just added to the table without deleting data from previous iteration.
So I tryed to use Clear Data Table activity for all iterations exept the first one, but the error Object reference not set to an instance of an object. appears.

How can I handle it?

Hi @Olivera_Kalinic ,

Its better if you initialize the DataTable first before it enters the loop like so:

Assign->

FinalnaTablea = New System.Data.DataTable()

Could you give that a try and let us know if it works out for you?

Kind Regards,
Ashwin A.K

Hi,

If you want to add datatable schema and data to the datatable at GenerateDataTable activity, it’s unnecessary to initialize or clear the datatable in advance. Probably it works, even if there are no activities before GenerateDataTable activity.

Regards,

It works, but now I have another problem:

During first iteration (after first GenerateDataTable activity) table finalnaTabela has 19 columns,

image

but during second iteration number of columns grows (after GenerateDataTable activity)

image

so I can’t use it in some other parts of the process.

Do you know what the cause might be?

Hi @Olivera_Kalinic ,

It would be best to verify if the data you are reading contains that many columns.
If you are confident that the columns are fixed, then you can simply reinitialize the DataTable after every iteration using the same snippet which was shared above:

New System.Data.DataTable()

Also, do give read on what @Yoichi -san has detailed above and see if that works out for you.

Kind Regards,
Ashwin A.K

I tryed, but doesn’t work.
I have several csv files, that I combine into one and convert to string.

Than trying to use Generate Data Table - after first iteration data table is created correctly, but after second GenerateDataTable data is just added to the fullfiled data table from first iteration (it seems that this activity doesn’t create new instance but just add new data to the previous one), and number of columns has growed.

@Olivera_Kalinic During first iteration as like you said it has 19 columns and during second iteration new data got added to the already existing data (which means new data got added to 19 columns)

To avoid this case, clear datatable after every iteration

Capture

Hi,

DataTable property of GenerateDataTable is OutArgument type as the following. This means it creates new instance and not affected by the previous DataTable content.

img20220318-1

I think perhaps you need to check input string for GenerateDataTable.

Regards,

Great, problem was the input string.
Thanks a lot :).

1 Like

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