How to manage Null DT so it does not throw NullReferenceException

Issue: I’m getting a NullReferenceExecption because my DT was not been populated. See screenshots


image

Since the logic may not populate the DT ( rare but its possible), I have implemented a check for null on the DT

Question: Despite the fact that I have a null check in place, why am I still getting the exception?

Use OrElse instead of Or, so that the second condition is not evaluated. Also, you might need to add the null check to the Else If part due to this issue: Activities - Else If

1 Like

Hi,

In this case, it’s necessary to use OrElse instead of Or because OrElse is short-circuit evaluation.

Regards,

1 Like

@Yoichi @efelantti
I did use orlse and added another check, but the error still wont go away.


See error below:

image

HI,

Unfortunately, it’s limitation of ElseIf activity as the following document.

Please use the following condition in ElseIf part, for example.

dt_preRegisteredPatlds isnot Nothing AndAlso dt_preRegisteredPatlds.Rows.Count=expectedNumOfAccts

Regards,

2 Likes

@Yoichi
Thanks for the reply, are you suggesting I change my “first condition”
dt_preRegisteredPatIds is Nothing orelse dt_preRegisteredPatIds.Rows.Count = 0 orelse not dt_preRegisteredPatIds.Rows.Count.Equals(expectedNumOfAccts)

This is where I was checking for a null DT and if the row count is not equal to some predefined expected value

HI,

According to limitation of ElseIF activity, the first condition and the second condition both are evaluated even if the first condition is true.

So, Not only the first condition should be modified but also the second one, as the following.

1st condition

dt_preRegisteredPatIds is Nothing orelse dt_preRegisteredPatIds.Rows.Count = 0 orelse not dt_preRegisteredPatIds.Rows.Count.Equals(expectedNumOfAccts)

2nd condition

dt_preRegisteredPatlds isnot Nothing AndAlso dt_preRegisteredPatlds.Rows.Count=expectedNumOfAccts

Regards,

1 Like

Hi @Yomi_Oluwadara

Assign DTHaveRows = IF(DT.RowCount>1,True,False) (DTHaveRows Var Type= Boolean)
If you Data Table has Values then it will return True otherwise it will return False

IF DT is NULL you can check using IF Condition
DT Is Nothing Or Else DT.Rows.Count = 0

Hope this will help you to understand better.

Cheers!

2 Likes

Hi @Nawazish_Ahmad
Better if you delete your advice.
The question was how to handle Null DT. Your proposal will not address it → if DT is Null then DT.RowCount will throw exception.

Cheers

P.S. Even after the update your advice is not any better :frowning: The correct answer was already given by @Yoichi

4 Likes

@Yoichi Thanks for the input, but I’m still not out the woods yet because I’m still getting the exception.
I output the content of the second DT ( see screenshots) and I’m sure that it has not been populated.

image
image




image

I also did a check on the length of the string output of the DT table, so I can terminate if the string is nothing, yet I got the same error


image

Is there any getting around this NULL DT issue ?

You need to use AndAlso instead of And in the “Else If - Condition”.

1 Like

As already indicated by @efelantti use AndAlso instead on And
The error is generated by the second part of the condition which is in case of using simple And also evaluated.
image

Cheers

1 Like

Thanks all, you were all accurate about the use of “And”

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