- Home /
Null Reference exception when loading json data to dictionary
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 [i].key, wordData.items [i].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...
Answer by Bunny83 · Apr 16, 2017 at 02:47 AM
Well, how does your JSON file look like? It has to look like this:
{
"items":[
{
"word":"someWordHere",
"meaning":"some meaning here"
},
{
"word":"someOtherWordHere",
"meaning":"some other meaning here"
},
]
}
If your JSON doesn't look like this you can't load it into that datastructure.
$$anonymous$$y JSON file looks like:
{
"DIPLOBLASTIC":"Characterizing the ovum when it has two primary ger$$anonymous$$allayers.",
"DEFIGURE":"To delineate. [Obs.]These two stones as they are here defigured. Weever.",
"LO$$anonymous$$BARD":"Of or pertaining to Lombardy, or the inhabitants of Lombardy.",
"BAHAIS$$anonymous$$":"The religious tenets or practices of the Bahais.",
"FU$$anonymous$$ERELL":"See Femerell.",
"ROYALET":"A petty or powerless king. [R.]there were at this time two other royalets, as only kings by hisleave. Fuller."
}
and after changing it to look like yours:
{
"items":[
{
"word":"DIPLOBLASTIC",
"meaning":"Characterizing the ovum when it has two primary ger$$anonymous$$allayers."
},
{
"word":"DEFIGURE",
"meaning":"To delineate. [Obs.]These two stones as they are here defigured. Weever."
},
{
"word":"LO$$anonymous$$BARD",
"meaning":"Of or pertaining to Lombardy, or the inhabitants of Lombardy."
},
{
"word":"BAHAIS$$anonymous$$",
"meaning":"The religious tenets or practices of the Bahais."
},
{
"word":"FU$$anonymous$$ERELL",
"meaning":"See Femerell."
},
{
"word":"ROYALET",
"meaning":"A petty or powerless king. [R.]there were at this time two other royalets, as only kings by hisleave. Fuller."
},
]
}
I now get an error ArgumentException: JSON parse error: Invalid value
$$anonymous$$aybe the Unity main parser doesn't like empty values at the end. Re I've the last comma after the last entry
i removed it and now i am back to the nullReferenceException error
Your answer
Follow this Question
Related Questions
JsonUtility with openweatherAPI 1 Answer
Unity json get data 2 Answers
Issues with JSONUtility 0 Answers
Multiple Cars not working 1 Answer