Runtime Error : Index out of range, when i press a button...

Runtime Error : Index out of range, when i press a button...

  
Hi,
I'm getting the following error and quite don't know why.....
I have this main screen where i have buttons that send info to other screens....

When i press a button it gives me this error....

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index at ssMainEntityInterface.Flows.FlowEntitiesFlow.ScrnEditEntity.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()


What can it be??

Best regards,

Diogo CS Cordeiro
If i have a link instead of a button, the error disappears....

But the problem is still there and i would like a button instead of a simple text link....
Hi Diogo,

If you're using a screen action along with that button, and that screen action ends with the node 'End' (I'm assuming that you're using OS platform version 4.x), then local variables and other stuff will remain with their current values (.NET postback procedure).

Now imagine that you're using a local variable as a Counter. If you do not explicitly initialize that counter to 0, within the preparation, and you increment it somewhere, then after clicking the button and doing the postback, the preparation will be executed again, but now the counter has already a value different than 0 that could somehow be out of range...

Within the preparation you can check if it's a postback or not by using the built-in function IsLoadingScreen(). If true, it's not a postback and therefore local variables will have their default values, otherwise you must reset those local variables explicitly (ex: Counter = 0).

Why did the link worked?
Because the link is a simple redirection (new HTTP request), and therefore could never be a postback. We can also make the button having the same behavior by making it going to the 'Current Screen' instead of using a screen action.

Hope this helps...

Best regards,

Duarte Gouveia
Actually, I was switching between screens when i noticed the error, so it wasn't a postback...

And now...well, i have a save button, and when i press it it gives me the same error......

I ran it on debug, and it doesn't enter the action flow....
I have no idea what can be happening....

One moment it worked, the other all buttons stop working....
Are you trying to access a record list directly?

Ex: recordListSample[5].sampleRecord.value

...

Can you submit your oml? If you prefer, send it by email to duarte.gouveia@outsystems.com
No, not indexing it...

I found out what was causing the problem, although don't know why it was happening....


I had several list boxes, conditioned in an if statement ...

something like : If ( not ListBox.List.Empty) -> show listbox .....

I had this in an editrecord....
Ok. Let me see if this is it...

You have a ListBox within an If widget, and outside it (ex: EditRecord) you an expression or condition that is testing if the ListBox that is inside that If widget is empty.

If that's the case then it does make sense that error.

When you put something within If widgets you're saying that specific content will only be rendered as HTML if the condition is matched, otherwise the content/objects will not exist, and therefore cannot be referenced (index out of range -> object is not available on the scope).

Instead of testing if ListBox1.List.Empty, you could test if the query that is going to fill in that ListBox is empty. Queries are always available on the scope (by design).

Duarte Gouveia
Thank you Duarte..

Yes, Using the query that fills the list box seems the best solution, which i have now implemented.

Best regards,

Diogo CS Cordeiro