Array index out of range
StreamReader fileReader = new StreamReader(filepath, true);
//read the text file
filecontent = fileReader.ReadToEnd();
//close the file when done reading
fileReader.Close();
//split the text file into lines
lines = filecontent.Split("\n"[0]);
//list of nodes and int's
List<Vector3> nodesList = new List<Vector3>();
List<int> intList = new List<int>();
int mode = 0;
Debug.Log(values.Length);
for (int l = 0; l < lines.Length;l++)
{
values = lines[l].Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
if( values[0] == "NODES")
{
mode = 1;
break;
}
if( values[0] == "TRIANGLES")
{
mode = 2;
break;
}
i get the error(Array index out of range) at the if(values[0] == Nodes)
i don't understand why it is giving the error.
I would be very thankfull if somebody could explain what i did wrong.
thank you in advance
Regarding the error itself: The error indicates that the value
array, exists, but it is empty.
If it were NOT empty, and had at least one items on it, then an index value of [0], would NOT be out of range.
e.g existant, but empty array
List<int> intList = new List<int>();
intList[0] = 42; //generates an out of range error
e.g. existant, NOT empty array
List<int> intList = new List<int>();
intList.Add(42);
intList[0] = 42; // we have added an element to the list, so this index is now valid.
intList[1] = 42; //generates an out of range error
The debugger is your best friend. $$anonymous$$ore than likely lines[1] is not being split up as expected. Debug this and you will see what lines[1] is and why it does not set values.
Answer by adriono · Aug 01, 2016 at 07:09 PM
Maybe Split
return empty array and you try to access to first element which just isn't exist. You should check it earlier:if(values.Length > 0)
. I hope this helps.
Your answer
Follow this Question
Related Questions
Accessing the array of a null item / Moving items in array 0 Answers
Fill Array Of Arrays With Method/Function 0 Answers
how to RemoveAt 2 Answers
Array index is out of range? 4 Answers
Array index is out of range C# 1 Answer