- Home /
ArgumentOutOfRangeException When Argument Is Not Out Of Range
So I'm getting an ArgumentOutOfRangeException
error that makes no sense to me. Whenever it tries to access a certain variable at a particular index in a List<>
, I run a check to see if the List<>
count is actually greater than or equal to the index I'm trying to access, but yet I still get the error. So I debugged it to see if maybe it was still searching for an out of range index, but it was searching for objects well within the List. Sometimes it would look for the 13th index in a List with 27 items, which means that an object should be there, yet I still get the error. I've checked all the times the error comes out, and every time is still gives me the error. So my question is whether or not ArgumentOutOfRangeException only refers to checking for an index outside of the range of the List, or to other things too?
ArgumentOutOfRangeExceptions are only thrown when array bounds are exceeded. There's probably a typo or logic error. Also, list.count has to be GREATER than the index (not greater-than-or-equal-to) because array indices are 0-based. Post some code?
Sorry, I didn't mean greater than or equal to. In any case, the count is greater than the index. Here's the code to find the index:
int column = 0;
int row = 0;
int a = 0;
while(a < nodes.Count){
int b = 0;
while(b < nodes[a].nodeCount){
if(nodes[a].nodes[b].node == current.node){
column = a;
row = b;
}
b++;
}
a++;
}
And here's the code where the error pops up:
if(row < (nodes[column].nodeCount-1)){
Node down = nodes[column].nodes[row+1];
//Some code in here
if(column < numberOfColumns){
Node downRight = nodes[column+1].nodes[row+1];
//Some code in here
}
}
So the first if-statement is checks to see if the row it wants to check actually exists (checks to see how many nodes is in the current column). Then it tries to assign the variable but then says the argument is out of range, although it can't be due to the check, and even if it could, the index it's checking is within the range of the list.
Answer by Loius · Jan 25, 2013 at 04:55 AM
nodeCount is a custom variable, which could be wrong.
Arrays are zero-based, so array[length-1+1] = array[length] is always null. Your +1 additions are pushing row and column out of bounds.
Checked the nodeCount variable, and it's all within that. And the checks make sure that it will never look for a node out of bounds. In any case, the error is popping up in the middle of the array, in which there is no risk of it being less than 0 or greater than the length of the array.
In the middle of both the column and row array?
Assu$$anonymous$$g that numberOfColumns==nodes.Count (if not, what's the difference?), you will run into an exception if you are in the last column, where column==numberOfColumns-1, because then you are accessing nodes[column+1], which is outside the array.
Yes in the middle of both arrays. And the difference is the number of columns is constant (in my case, 27) and nodes.Count is the number of nodes in the column (the # of rows).
That can't be right, as according to your code your first index (and therefore nodes.Count is #columns) selects the columns, and your second index (nodes[column].nodes.Count is #rows) is for the rows. How many rows do you have?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
A respawn script for enemies. 1 Answer
How to Repair Argument is Out of Range 1 Answer
List Works But Still Get Argument Out of Range Exception 1 Answer