NullReferenceException

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
ShivaFang
Posts: 5
Joined: Tue Feb 24, 2015 8:48 pm

NullReferenceException

Post by ShivaFang »

Been using EasySave2 for about a week and it worked fine. When I saw a new version today I updated and I'm now getting an error;

NullReferenceException: Object reference not set to an instance of an object

Code: Select all

ES2Writer.WriteHeader (System.String tag, Key collectionType, .ES2Type valueType, .ES2Type keyType)
ES2Writer.Write[Data] (IdleArtificer.Data param, System.String tag)
ES2.Save[Data] (IdleArtificer.Data param, System.String identifier)
IdleArtificer.AutoSave.SaveData () (at Assets/Scripts/MonoBehaviours/AutoSave.cs:32)
IdleArtificer.AutoSave.Update () (at Assets/Scripts/MonoBehaviours/AutoSave.cs:25)
My code that starts this is;

Code: Select all

ES2.Save(Data.current, "IdleArtificer/Data");
I've also tried it without the /. Data.current is an instance of the Data class which contains all my data I want to save - I have it all set up with the automatic class hookup function.

I am using a webplayer build with the default (playerprefs) saving. (I'm making a simple idle game that doesn't need extensive server-side data managing)
ShivaFang
Posts: 5
Joined: Tue Feb 24, 2015 8:48 pm

Re: NullReferenceException

Post by ShivaFang »

I decided to try to re-do the Auto Class in the 'Manage Types' to see if that fixed it, and it did for Data. But now I'm getting the same error for one of my other classes (Data contains 3 dictionaries, one of each other class). When I tried to redo the ShipValues information for Easy Save it did not fix it.

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
ES2Writer.Write[ShipValues] (IdleArtificer.ShipValues param)
ES2UserType_IdleArtificerData.Write (System.Object obj, .ES2Writer writer) (at Assets/Easy Save 2/Types/ES2UserType_IdleArtificerData.cs:21)
ES2Writer.Write[Data] (IdleArtificer.Data param, .ES2Type type)
ES2Writer.Write[Data] (IdleArtificer.Data param, System.String tag)
ES2.Save[Data] (IdleArtificer.Data param, System.String identifier)
IdleArtificer.AutoSave.SaveData () (at Assets/Scripts/MonoBehaviours/AutoSave.cs:32)
IdleArtificer.AutoSave.Update () (at Assets/Scripts/MonoBehaviours/AutoSave.cs:25)
Here is the E2Type code that the 'Manage Types' feature generates;

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IdleArtificer;

public class ES2UserType_IdleArtificerShipValues : ES2Type
{
	public override void Write(object obj, ES2Writer writer)
	{
		IdleArtificer.ShipValues data = (IdleArtificer.ShipValues)obj;
		// Add your writer.Write calls here.
		writer.Write(data.id);
		writer.Write(data.stringMode);
		writer.Write(data.active);
		writer.Write(data.updateCycle);
		writer.Write(data.travelTime);
		writer.Write(data.modeTime);
		writer.Write(data.store);
		writer.Write(data.rankProgress);
		writer.Write(data.payment);
		writer.Write(data.tribute);
		writer.Write(data.resTribute);
		writer.Write(data.earnTribute);
		writer.Write(data.deliverTribute);
		writer.Write(data.rank);
		writer.Write(data.nextRank);
		writer.Write(data.rankMultiplier);
		writer.Write(data.awayTime);
		writer.Write(data.production);
		writer.Write(data.locked);

	}
	
	public override object Read(ES2Reader reader)
	{
		IdleArtificer.ShipValues data = new IdleArtificer.ShipValues();
		// Add your reader.Read calls here and return your object.
		data.id = reader.Read<System.String>();
		data.stringMode = reader.Read<System.String>();
		data.active = reader.Read<System.Boolean>();
		data.updateCycle = reader.Read<System.Single>();
		data.travelTime = reader.Read<System.Single>();
		data.modeTime = reader.Read<System.Single>();
		data.store = reader.Read<IdleArtificer.ExponentNumber>();
		data.rankProgress = reader.Read<IdleArtificer.ExponentNumber>();
		data.payment = reader.Read<IdleArtificer.ExponentNumber>();
		data.tribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.resTribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.earnTribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.deliverTribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.rank = reader.Read<System.Int32>();
		data.nextRank = reader.Read<IdleArtificer.ExponentNumber>();
		data.rankMultiplier = reader.Read<System.Double>();
		data.awayTime = reader.Read<System.Single>();
		data.levelUp = reader.Read<System.Boolean>();
		data.production = reader.Read<IdleArtificer.ExponentNumber>();
		data.locked = reader.Read<System.Boolean>();

		return data;
	}
	
	/* ! Don't modify anything below this line ! */
	public ES2UserType_IdleArtificerShipValues():base(typeof(IdleArtificer.ShipValues)){}
}
EDIT: In case it matters, the IdleArtificer.ExponentNumber class only saves an Int and a Double. It represents numbers that are multiplied by powers of 100 (idle games tend to grow exponentionally with the resources collected)
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: NullReferenceException

Post by Joel »

Hi there,

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.

If this doesn't fix your problem, would you be able to send me the following so I can try to replicate the problem:
  • Your Data class file.
  • A copy of the ES2Init.cs file found in Assets/Easy Save 2/.
  • Which version of Unity you're using.
All the best,
Joel
ShivaFang
Posts: 5
Joined: Tue Feb 24, 2015 8:48 pm

Re: NullReferenceException

Post by ShivaFang »

That fixed it, thanks. I thought that button was for if I added a new class and it wasn't showing up on the list and it needed to repopulate the list.

I'm having other errors now from when I redid my ShipValues saving ( I saved values that I shouldn't have .. or didn't save something I should have.. and it's throwing errors at me in every direction) but that has more to do with my bad coding than your software.

Thanks for your help!

(EDIT - yup.. looks like I saved a public enum, which gummed up the works. I had to parse the enums as strings in order to get them to save well and I clicked the wrong thing when I was trying to work around the other error.)
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: NullReferenceException

Post by Joel »

Glad to be of assistance! Let me know if there's anything else I can help you with.

- Joel
ShivaFang
Posts: 5
Joined: Tue Feb 24, 2015 8:48 pm

Re: NullReferenceException

Post by ShivaFang »

Ugh. Actually it's my string ID that's not being saved/loaded properly. I even put in debug messages to see what is being loaded/saved.

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IdleArtificer;

public class ES2UserType_IdleArtificerShipValues : ES2Type
{
	public override void Write(object obj, ES2Writer writer)
	{
		IdleArtificer.ShipValues data = (IdleArtificer.ShipValues)obj;
		// Add your writer.Write calls here.
		writer.Write(data.id);
		Debug.Log ("Wrote:  "+data.id);
		writer.Write(data.stringMode);
		writer.Write(data.active);
		writer.Write(data.updateCycle);
		writer.Write(data.travelTime);
		writer.Write(data.modeTime);
		writer.Write(data.store);
		writer.Write(data.rankProgress);
		writer.Write(data.payment);
		writer.Write(data.tribute);
		writer.Write(data.resTribute);
		writer.Write(data.earnTribute);
		writer.Write(data.deliverTribute);
		writer.Write(data.rank);
		writer.Write(data.nextRank);
		writer.Write(data.rankMultiplier);
		writer.Write(data.awayTime);
		writer.Write(data.production);
		writer.Write(data.locked);

	}
	
	public override object Read(ES2Reader reader)
	{
		IdleArtificer.ShipValues data = new IdleArtificer.ShipValues();
		// Add your reader.Read calls here and return your object.
		data.id = reader.Read<System.String>();
		Debug.Log ("Read:  "+data.id);
		data.stringMode = reader.Read<System.String>();
		data.active = reader.Read<System.Boolean>();
		data.updateCycle = reader.Read<System.Single>();
		data.travelTime = reader.Read<System.Single>();
		data.modeTime = reader.Read<System.Single>();
		data.store = reader.Read<IdleArtificer.ExponentNumber>();
		data.rankProgress = reader.Read<IdleArtificer.ExponentNumber>();
		data.payment = reader.Read<IdleArtificer.ExponentNumber>();
		data.tribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.resTribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.earnTribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.deliverTribute = reader.Read<IdleArtificer.ExponentNumber>();
		data.rank = reader.Read<System.Int32>();
		data.nextRank = reader.Read<IdleArtificer.ExponentNumber>();
		data.rankMultiplier = reader.Read<System.Double>();
		data.awayTime = reader.Read<System.Single>();
		data.levelUp = reader.Read<System.Boolean>();
		data.production = reader.Read<IdleArtificer.ExponentNumber>();
		data.locked = reader.Read<System.Boolean>();

		return data;
	}
	
	/* ! Don't modify anything below this line ! */
	public ES2UserType_IdleArtificerShipValues():base(typeof(IdleArtificer.ShipValues)){}
}
The Debug.Log on the write side works, but when it loads it's like it's loading either a null or an empty string. It puts out "Read:"

I checked all my other classes to make sure that it's loading and saving the same number and types of values (otherwise the bit stream is out of sync.. I don't know what the proper term is.. but you know what I mean I hope)

EDIT: It looks like it reads one, but then fails to read any others. When I double-checked the saving info it definitely does not save any empty or null strings. Very odd.
ShivaFang
Posts: 5
Joined: Tue Feb 24, 2015 8:48 pm

Re: NullReferenceException

Post by ShivaFang »

Found it! It was trying to load a boolean that it wasn't saving. (Seeing that it loaded the first one correctly made me realise that there _must_ be something wrong with that class's saving as it'd be trying to load a dictionary.) It's the 'LevelUp' boolean - something I toggle to tell the UI to play the LevelUp message. I realised after I auto-created the class data I didn't need it and missed it on the bottom half.

Sorry for rambling at you with my problems that have nothing to do with your code but mine!
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: NullReferenceException

Post by Joel »

Not a problem! It's usually the smallest problems which are the hardest to find ... I know that's the case when I'm coding anyway! :D

- Joel
Locked