GetKeys Gives a null reference error

Discussion and help for Easy Save 3, The Complete Save Game & Data Serializer System for the Unity Engine
Post Reply
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

GetKeys Gives a null reference error

Post by Recktardeded »

Hello, i use this function:
public void Elements_LoadAll(List<string> allElementsList)
{
if (allElementsList == null)
{
allElementsList = new List<string>();
}
foreach (string key in ES3.GetKeys("Elements.es3"))
{
if (!allElementsList.Contains(key))
allElementsList.Add(key);
}
}
And if there are no elements in the file "Elements.es3" i get a null reference error. Could i add a check to not avoid the error?
User avatar
Joel
Moodkie Staff
Posts: 5006
Joined: Wed Nov 07, 2012 10:32 pm

Re: GetKeys Gives a null reference error

Post by Joel »

Hi there,

I don't appear to be getting a null reference exception from ES3.GetKeys on a file with no keys. I've tested this with the following code in the latest version:

Code: Select all

using UnityEngine;

public class GetKeysTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        ES3.DeleteFile();
        ES3.Save("key1", 123);
        ES3.Save("key2", 123);

        ES3.DeleteKey("key1");
        ES3.DeleteKey("key2");
        // File will now be "{ }"
        
        var keys = ES3.GetKeys(); // Returns an empty array without error.
    }
}
It will throw a FileNotFoundException if the file doesn't exist but this is expected behaviour. Are you sure you're using the latest version? Please could you post the error with full stack trace as there's a multitude of reasons a NullReferenceException could happen.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply