i am trying to load a json file into a dictionary but i get a NullReferenceException error anytime it gets to the for loop. Here is the code I am using for loading the json file
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class CategoryController : MonoBehaviour {
private Dictionary<string, string> wordDictionary;
private string fileName = "testdictionary.json";
void Start () {
LoadGameDictionary();
}
void LoadGameDictionary()
{
wordDictionary = new Dictionary<string, string>();
string filePath = Path.Combine(Application.streamingAssetsPath, fileName);
if (File.Exists(filePath))
{
Debug.Log("File found");
string dataAsJson = File.ReadAllText(filePath);
WordsData wordData = JsonUtility.FromJson<WordsData>(dataAsJson);
for (int i = 0; i < wordData.items.Length; i++)
{
wordDictionary.Add(wordData.items _.key, wordData.items *.value)*_
}
}
else
{
Debug.LogError(“file not found”);
}
}
}
these are the data classes i am using
[System.Serializable]
public class WordItems
{
public string word;
public string meaning;
}
[System.Serializable]
public class WordsData{
public WordItems[] items;
}
thank you in advance…