Object reference not set to an instance of an object - During for each and If Command

Hi,

I am trying to achieve the following

  1. Read Excel - read range and assign a datatable variable
  2. Get row value for a particular column for each row - (For each activity and Get datarow activity)
  3. Use If conditional statement that if columnvariable.contains(“String”) then perform certain action

The problem with above is that it throws an error stating that the object reference is not set to an instance of an object in the If command stage

I rechecked my Getdatarow variable with write line to see if its getting the values from the excel, it is getting all the values but still its throwing me an error in the “IF” statement. Any help is appreciated

1 Like

What is the condition that you are using?

You can follow this approach too

  1. read range set variable Dt
  2. Use for each row activity by using variable Dt
  3. condition (in if activity) : Not String.IsNullOrEmpty(row(Column_name).ToString)
    In then put your activities which you want to execute if it contains string

getrowvariable.contains(“string”) then perform some actions.

The row value contains text with spaces.

Did you try this approach

What does the above if statement do, can you elaborate a little please . thanks

This step is fine buddy
Then this one as well

But here in the if condition we need small changes to be implemented
like

before searching for contains we need to check with the value whether it is null or not
like this
NOT String.IsNullOrEmpty(columnvariable)
Where String is a class and IsNullOrEmpty is a method that helps us to check whether the string has null value or not…if it has null value it will give us True or False if not
as we have used NOT logical operator which gives the invert of the result from String.IsNullOrEmpty,
if the above condition passes it means there is no null value and it will go to THEN part where we can use another if condition like this
columnvariable.contains(“String”)

Cheers @Ashish_Mehra

Thanks @Palaniyappan his works perfectly, just for future information, the “IF” statement while iterating through the rows always throws an error if the value is Null within a cell ? , I just wanted to know for future that If I am using an IF statement is it preferable to first filter out the non-null values ?

1 Like

Great buddy

well not always but i would suggest to go for a filteration with null values even though you are clear with what output you will be getting…because sometimes we might get the null value out of data fetching and is always a good practice to include such filteration
or
to be better in business process template , we can simply use a single if condition with contains in it, but we need to be sure that is kept in a try block of try catch activity as if any errors occurs as how we get now, it will go to catch block where we can make a validation with null value and proceed with the same step from if condition with contains in it…this looks same to the above one, but the thing is we can use a log message out here next if condition that we have got a exception because of this null value that lets us know that we had such error while processing…and it helps us to debug if we need

Cheers buddy @Ashish_Mehra

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