C#C
C#2y ago
8 replies
Mek

✅ Object reference not set to an instance of an object?

public class UserGenerator
{
    private static Random rand = new();

    private static List<string> SelectRandomNames(int count)
    {  
        List<string> selectedNames = new();

        for (int i = 0; i < count; i++)
        {
            int index = rand.Next(0, Names.Count);
            string name = Names[index];
            selectedNames.Add(name);
        }

        return selectedNames;
    }
}
I keep getting a run-time error of Object reference not set to an instance of an object on line int index = rand.Next(0, Names.Count); and I'm not sure why.
Was this page helpful?