getting an index out of range exception, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

getting an index out of range exception, why?

this code returns an error that somewhere im pointing to an index in an array that is out of range, can anyone tell me where? update: solved

2nd Mar 2018, 1:46 PM
hinanawi
hinanawi - avatar
6 Answers
+ 3
Hi, your input array does not have 100 elements. The condition of your for loop: for(int i = 0; i < 100; i++) requires your for loop body code to run up to 100 times. The lines using: input[i] in the body code, however, cannot run and throws exception as the array input does not have 100 elements. You have to refactor your code. I am not sure what you are trying to do as I could not interpret from the naming of functions and variables, but the exception is caused by your for loop stated above; whenever it tries to call input[i] to be more precise and when the index i is bigger than the size of the input array. Hope this helps :)
2nd Mar 2018, 2:56 PM
Deddy Tandean
+ 5
I don't even know what you're trying to do, but imo you need to focus on bfjsIterate method and carefully observe the value of cIndex member, as I see it, the cIndex is the one used to address index pointing there.
2nd Mar 2018, 2:52 PM
Ipang
+ 5
@Hinanawi, I'm no good in C#, yes I saw that bound guard too, not too sure why or what, I only tested by commenting one method call, each once at a time, and got the exception upon uncommenting the bfjsIterate, unfortunately Code Playground doesn't help in pointing the line where it occurred, I can't seem to help any further, I would like to see you bust that bug, and when you do, please share it here as a lesson for us, okay. Best of luck : )
2nd Mar 2018, 3:03 PM
Ipang
+ 2
no problem, it is a learning process :)
2nd Mar 2018, 3:12 PM
Deddy Tandean
+ 1
@lpang yes i see that's probably it, but the cIndex value is default at 0, and i have if statements to prevent it from going lower than 0 and over 99. will probably fire this up in visual studio later to get a more detailed error report
2nd Mar 2018, 2:54 PM
hinanawi
hinanawi - avatar
0
@Deddy Tandean ah yes thank you so much. i must have gotten confused while coding and accidentally coded the loop as if i was trying to iterate through the elements of the cell array not taking lengths into account. thanks.
2nd Mar 2018, 3:05 PM
hinanawi
hinanawi - avatar