Serializing and passing on a public string...

I’m using FullSerializer to serialize a string and then upload it to a cloud.

So far, this is what I’ve got for the class that needs serializing:

[System.Serializable]
    public class SavedGame
    {
        public string dataToSave ="";
    }

    [System.Serializable]
    public class XboxOneDataClass
    {
        public SavedGame savedGames = new SavedGame();
    }

dataToSave is the string containing EVERYTHING that needs saving in my game.

In my save code, I’m using this:

Debug.LogError("Attempting to save cloud data...");
            Type myType = Type.GetType("XboxOneDataClass");
            string serializedData = MyFullSerializer.Serialize(myType, xboxOneData);

            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(serializedData);
            GameSaveContainer container = gameSaveProvider.CreateContainer(c_saveContainerName);

            // To store a value in the container, it needs to be written into a buffer, then stored with
            // a blob name in a Dictionary.
            DataWriter writer = new DataWriter();
            writer.WriteBytes(bytes);
            IBuffer dataBuffer = writer.DetachBuffer();
            var updates = new Dictionary<string, IBuffer>();
            updates.Add(c_saveBlobName, dataBuffer);
            Debug.LogError("Data saved to buffer...");
            GameSaveOperationResult result = await container.SubmitUpdatesAsync(updates, null, c_saveContainerDisplayName);
            Debug.LogError("Submitting save game to cloud...");
            if (result.Status == GameSaveErrorStatus.Ok)
            {
                Debug.LogError("SUCCESSFULLY Saved data : " + bytes);
            }
            else
            {
                Debug.LogError("SaveData FAILED: " + result.Status);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("SaveData failed: " + ex.Message);
        }

The error shown is “SaveData failed: Value cannot be null. Parameter name: key” in regards to the dataToSave string.

The dataToSave string is created in another script.

My question is, how do I ensure that that string isn’t null and is successfully passed to be submitted for uploading?

I’m not a genius at coding so please go easy on me! :wink:

Any help would be VERY much appreciated, thank you kindly :slight_smile:

This is no different than a normal NullReferenceError.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Once you find what is null, your two choices are generally:

  • ensure it is NOT null

  • check if it is null and do something else that doesn’t require it to be non null.