I have IDictionary<string, int>. I need to get keys and values from dictionary in string format and add into string[] array. I need to do it because I want to pass that text in function and that function takes string array. I have that code for getting value:
foreach (KeyValuePair<string, int> kvp in dict)
{
dataList.Add(kvp.Key + " " + kvp.Value.ToString());
}
I've created list and add in that list keys and values. After I was thinking to create for loop and from list add elements into array, but have error. Loop:
string[] txtArr;
for (int i = 0; i < dataList.Count; i++)
{
txtArr[i] = dataList[i];
}
Error: System.NullReferenceException. I don't understand where is the problem. From what I read about that error, I understood it raised because something is null
. But if I'm trying print in console dictionary or list all is OK, there isn't null
, also i
is int
and = 0
. Where is the problem?