NullReferenceException - Dictionary

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
pvonbuskirk
Posts: 2
Joined: Mon Jul 07, 2014 7:48 pm

NullReferenceException - Dictionary

Post by pvonbuskirk »

I have been using EasySave2 for a long time and it works great for me. It's very useful, especially the simplicity of the web saving.

Just yesterday, was adding a Dictionary to my data structure and I am getting the following Null Reference Exception:

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
ES2Writer.WriteHeader (System.String tag, Key collectionType, .ES2Type valueType, .ES2Type keyType)
ES2Writer.Write[String,Object] (System.Collections.Generic.Dictionary`2 param, System.String tag)
ES2.Save[String,Object] (System.Collections.Generic.Dictionary`2 param, System.String identifier)
testScript.Start () (at Assets/testScript.cs:17)
This is just a simple Dictionary. I created a test project to isolate the issue, with this in Start():

Code: Select all

Dictionary<string, object> dataDict = new Dictionary<string, object>();
dataDict.Add("keyname1", "valueString");
dataDict.Add("keyname2", 100);
		
ES2.Save(dataDict, "data/datafile.txt" + "?tag=dictionary_01");
I read the topic here: http://www.moodkie.com/forum/viewtopic. ... ce+not+set, which seemed similar, though I'm not using a custom data type. And I followed the suggestion you made, in case it was related to my issue:
Try going to Assets > Easy Save 2 > Manage Types ... and pressing the Refresh ES2Init button at the top. Sometimes when updating the Asset Store accidentally suppresses the script which automatically updates the ES2Init file. The errors you're getting would definitely point at this.
But, this did not help me.
I don't get this error with other types: List, string, int, etc.

I am currently using Unity v4.6.1f1
And it looks like I'm using ES2 v2.6.2 (I avoid updating in the middle of a project if I can)
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: NullReferenceException - Dictionary

Post by Joel »

Hi there,

The key and value type of your Dictionary needs to be a Supported Type, but it's currently set to object.

For Easy Save to serialize the Dictionary, it must be type safe. i.e. it needs to be either a Dictionary<string, int> or a Dictionary<string, string>, it can't be Dictionary<string, object>. If you need to store both, you'll need to separate it out into two Dictionaries for each data type you wish to store.

Hope this helps.

All the best,
Joel
pvonbuskirk
Posts: 2
Joined: Mon Jul 07, 2014 7:48 pm

Re: NullReferenceException - Dictionary

Post by pvonbuskirk »

I see. Of course. That will help.
I was sure it was something simple I was doing wrong.

Thanks a lot for the quick reply!
Locked