Index was out of the bounds of array: comes when I replace extraction results

Hi All,

When I use some statement like this io_ExtractionResults.ResultsDocument.Fields(10).Values(0).Value I get an error saying index was out of the bounds of array.

to avoid this, I tried adding an “if” condition saying io_ExtractionResults.ResultsDocument.Fields.Count > 10

and another method not string.IsNullOrEmpty(io_ExtractionResults.ResultsDocument.Fields(10).Values(0).Value) and io_ExtractionResults.ResultsDocument.Fields(10).Values(0).Value.ToUpper.Contains(“string value”)

however I am still getting this error.

That’s because when you have…

If someExpression And anotherExpression

…both expressions are processed, then the result is generated. So even if the first expression is false, it still has to process the second expression to generate the boolean result.

In this case you should use AndAlso. It’s what is called “short circuiting.” If the first expression is false it doesn’t process the second expression.

If someExpression AndAlso anotherExpression

Thanks Paul,

I tried and added this, still getting this error even after changing “and” to “andAlso” :face_with_spiral_eyes:

Hi @sharon.palawandram ,

Was the Expression checked in the Debug Panel ?

Check the indices/count of the Values property as well :

io_ExtractionResults.ResultsDocument.Fields(10).Values.Count

Hi Arpan, for this example, there’s surely more than 10 index fields in the document, strange that it comes like this. Can it be solved if we don’t add the field as an index but rather as a field= “” value?

What is the error you are getting?

same, Index was out of the bounds of the array

How about a LINQ filter, as we could assume the fieldName?

field name will always be static. how does a linq filter work here?

I have sent you a PM

1 Like

You’re checking if .Fields.Count is greater than 10 and it is, but you’re getting .Fields(10).Values(0) so the index error probably means there are no elements in .Values

Change your expression to…

out_ExtractionResults.ResultDocument.Fields.Count > 10 AndAlso out_ExtractionResults.ResultDocument.Fields(10).Values.Count > 0 AndAlso string.IsNullOrEmpty(out_ExtractionResults.ResultsDocument.Fields(10).Values(0).Value)

1 Like

Beta / untested:

myER.ResultsDocument.Fields.Where(Function (x) x.FieldName.Equals("myFieldName")).SelectMany(Function (x) x.Values).Select(Function (x) x.Value).FirstOrDefault

OR As Query Syntax

strValue =

(From f in out_ExtractionResults.ResultDocument.Fields
Where f.FieldName.Equals("myFieldName")
From v in f.Values
Select s = v.Value).FirstOrDefault()
1 Like

@sharon.palawandram ,

Checking it out from our end we do get value when the index and the value is present :
image

When the Values for the field are missing, then we do not have any values to index to.

image

Also, Maybe a check with the below post :

Additionally on handling of missing values :

io_ExtractionResults.ResultsDocument.Fields.FirstOrDefault(Function(i As ResultsDataPoint) i.FieldName = "Field Name" AndAlso Not(i.IsMissing)) is Nothing

image

We can then access the Value after the Above condition is resulted false :

io_ExtractionResults.ResultsDocument.Fields.FirstOrDefault(Function(i As ResultsDataPoint) i.FieldName = "Field Name" AndAlso Not(i.IsMissing)).Values(0).Value

image

2 Likes

Thanks Arpan,I tried this and it worked good for this logic

io_ExtractionResults.ResultsDocument.Fields.FirstOrDefault(Function(i As ResultsDataPoint) i.FieldName = “Field Name” AndAlso Not(i.IsMissing)) is Nothing

but for this it failed again.

io_ExtractionResults.ResultsDocument.Fields.FirstOrDefault(Function(i As ResultsDataPoint) i.FieldName = “Field Name” AndAlso Not(i.IsMissing)).Values(0).Value

Thanks Paul, this worked. Can you let me know the logic behind it? I thought it might throw an error since we are still assigning indexed values.

The AndAlso means the expression after it is only processed if the expression before it returns true. So out_ExtractionResults.ResultDocument.Fields(10).Values.Count > 0 is only processed if out_ExtractionResults.ResultDocument.Fields.Count > 10 is true, and string.IsNullOrEmpty(out_ExtractionResults.ResultsDocument.Fields(10).Values(0).Value) is only processed if the previous two expressions are true.

1 Like

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