0

I am getting the index out of range exception. I debuged it and it is specifically at the part of "s[i].AddRange()" The "GetFiles()" method is just the "Directory.GetFiles()" but it can accept multiple file endings.

static List<List<string>> directories
            {
                get
                {
                    Debug.Log(folders.Length);
                    List<List<string>> s = new List<List<string>>();
                    for (int i = 0; (i < folders.Length); i++)
                    {
                        s[i].AddRange(GetFiles(folders[i], "*.png,*.jpg,*.bmp,*.tif,*.tga,*.psd").ToList());
                        s[i].AddRange(GetFiles(folders[i], "*.prefab").ToList());
                    }
                    return s;
                }
            }
4
  • 1
    You have created an empty list and are trying to access invalid positions within it Commented Apr 23, 2017 at 0:59
  • I checked for that in the 7th line in the "for" condition. Commented Apr 23, 2017 at 1:01
  • No s[i] is the problem Commented Apr 23, 2017 at 1:02
  • OK I see what you mean. How do I fix it? Commented Apr 23, 2017 at 1:04

2 Answers 2

1

When you do this, you get an empty box with the potential to store boxes of strings, but there's nothing in it yet. Trying to access element i causes it to throw the exception you're seeing.

List<List<string>> s = new List<List<string>>();

Before you can access an item, you need to add an item.

s.Add(new List<string>());

In your case though, I wouldn't bother adding an empty list and then accessing it using the index. Just do this instead:

List<List<string>> s = new List<List<string>>();
for (int i = 0; (i < folders.Length); i++)
{
    var list = GetFiles(folders[i], "*.png,*.jpg,*.bmp,*.tif,*.tga,*.psd").ToList();
    list.AddRange(GetFiles(folders[i], "*.prefab").ToList());
    s.Add(list);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure what's happening. Everyone is suggesting the "s.Add(new List<string>());" but for some reason it's not working. Also, for your second suggestion, the way I'm going to use the list requires it to be done the way I'm showing you.
I'm sorry, I assumed your last code block was what everyone else was suggesting. I looked at it again, pasted the code and voila, the problem was solved. Thank you.
0

You have newed a List> but not added any new lists.

Here is a very stripped down for loop which should help.

List<List<String>> s = new List<List<string>>();
for (int i = 0; i < 10; ++i) {
    s.Add(new List<String>());
    s[i].AddRange(new String[] { "a", "b", "c" });
}

Updated to fix the problem in my original answer.

5 Comments

Actually he needs a s.Add(new List<string>());.
Sorry, both of these still create an index out of range exception in the "s[i]" part.
You have to add a new list before you access s[i]. And a new list will need to be added for each iteration of the for loop to make sure that one is available to access.
It is the "s.Add(new List<string>());" is on the line before i use s[i].
Then is your folders array empty or null? I don't know why s[i] would throw an exception if you explicitly add a new list every iteration of the loop. It doesn't look like your folders should be null but I have no idea at this point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.