Grasshopper

algorithmic modeling for Rhino

Index in my list that's looped through by a for-loop is out of bounds. It must be non-negative and less than the size of the collection

Below is a code snippet of my ShowForm() method, which is used with my winforms.

The error I am receiving is:

Some diagnostic information:

1. I know that the if-statement is hit, because of a MessageBox.Show() method I put there. I am thinking the for-loop is somehow indexing out, but I'm not quite sure why it is doing so.

2. The x.Count: is 7

3. Doing MessageBox.Show(Params.Index[0].ToString()); to check if Params.Index[0] is empty crashes my program every time without fail. What could that mean?

Views: 513

Replies to This Discussion

Hi,

Well the reason it's crashing is because you're trying to access an array in an invalid index.

You're accessing only two arrays Params.Input & x.

The access to x seems alright, tho you need to notice that you're accessing from index 0 to count - 1. which means if you have only 1 element in the array you're not accessing anything. you should lose the '-1' because you're already limiting the loop to a number that is < than count. (using -1 would be logical if you're limiting it by <=)

You need to check for Params.Input length before accessing it, my guess is that it's empty and therefor accessing it in the 0 index raises an exception. and that is your bug

for your 3rd question -if you have an exception that is not handled Rhino will exit at once. Accessing Param.Input[0] WILL raise an exception if Param.Input is empty or null. 

I suggest that next time before accessing any arrays make sure they're not null/empty and that your index is valid for that array.

:)

You've also noticed that in the for loop that you're accessing 'x' you're not going through all the elements and quitting the loop before the last element?

If not, re-read what i wrote regarding that loop in my first reply.

H.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service