- Home /
"Object reference not set to an instance of an object" when attempting to parse Dictionary of Lists with JSONFx, MiniJSON
I'm attempting to parse some JSON using the JSONFx plugin that I found here.
My JSON looks something like this.
{
"tweakableSliders" : [
{
"Name" : "DrPepper",
"Min" : 50.0,
"Default" : 62.0,
"Max" : 100.0
},
{
"Name" : "7up",
"Min" : 22.0,
"Default" : 120.0,
"Max" : 250.0
},
],
"tweakableToggles" : [
{
"enableBananas" : false
},
{
"enableApples" : true
}
]
}
And so I'm wanting to get a Generic Dictionary of Generic Lists as a result. I'm trying to use the following line:
var tweakable : TextAsset = Resources.Load("tweakable");
var collection = JsonReader.Deserialize(tweakable.text) as Dictionary.<String, List.<Object> >;
But this gives me the error: NullReferenceException: Object reference not set to an instance of an object
If I do something like, var collection = JsonReader.Deserialize(tweakable.text) as Dictionary.<object>;
it'll compile but if I attempt to cast that Object to a List and use it as a List I'll have the same error.
I'm very new to JSON and clearly not understanding something.
EDIT: I have the same issue with MiniJSON so I guess I'm not understanding the restrictions of these libraries/JSON or Unityscript (I'm a beginner)?
P.S. If there's some other JSON library that might be better for what I want to do please suggest it.
Actually I was apparently wrong?! I have no idea what I was doing before but I can cast the Object to a List..
var collection = Json.Deserialize(tweakable.text) as Dictionary.<String, Object >;
for (var item in collection.Values)
{
var itemCollection = item as List.<Object>;
Debug.Log(itemCollection.Count);
}
This will work, which is great, but I'm still left wondering why I can't write:
var collection = Json.Deserialize(tweakable.text) as Dictionary. < String, List.<Object> >;
Before I go into more detail, do you already understand what the error actually means in a general sense? Like can you fix that error if it weren't a JSON error?
$$anonymous$$y interpretation would be that the object is null, but I'm trying to call a function on null and so there's an error. I have a C++ background and I'm a newbie to Unityscript and Javascript so I may be wrong.
I can get it working for $$anonymous$$iJSON but I still can't get similar code to work with JsonFX
ie. The following gives me the error: NullReferenceException: Object reference not set to an instance of an object
var reader = new JsonReader(tweakable.text);
var collection = reader.Deserialize() as Dictionary.<String, Object>;
for (var item : Object in collection.Values)
{
var list = item as List.<Object>;
Debug.Log(list.Count);
}
Exactly. The object is referencing an object that simply isn't there. So I would check to see if your file is being received. $$anonymous$$aybe throw the whole thing in a try/catch block, and catch a filenotfound exception? That would at least tell you if you're having problems there. I'd need to see more of your script to give you a detailed answer, though.