Running Into Null Reference Exception

So I followed the List Files in A Directory video, but now I am running into Null Reference exceptions. I am also unable to continue the game. I added the mini fix at the end of the New Game and PlayerPrefs Video. This is the error message I am getting

NullReferenceException: Object reference not set to an instance of an object
RPG.UI.SaveLoadUI.OnEnable () (at Assets/Scripts/UI/SaveLoadUI.cs:23)

Line 23 is this one:

foreach (string save in savingWraper.ListSave())
 {
               GameObject buttonInstance =  Instantiate(buttonPrefab, contentRoot);
                TMP_Text textComp = buttonInstance.GetComponentInChildren<TMP_Text>();
                textComp.text = save;
               Button button = buttonInstance.GetComponentInChildren<Button>();
                button.onClick.AddListener(() =>
                {
                    savingWraper.LoadGame(save);
                });
            }

I added some debug logs and when I click the LOAD button I get my debug code here showing up.

if (!GetComponent<SavingSystem>().SaveFileExists(GetCurrentSave()))
            {
                Debug.Log("NO SAVING SYSTEM");
                return;
            }

my Continue game looks like this

public void ContinueGame()
       {
           if (!PlayerPrefs.HasKey(currentSaveKey))
           {
               Debug.Log("NO KEY");
               return;
           }
           if (!GetComponent<SavingSystem>().SaveFileExists(GetCurrentSave()))
           {
               Debug.Log("NO SAVING SYSTEM");
               return;
           }
           StartCoroutine(LoadLastScene());
       }

Load Game is

public void LoadGame(string saveFile)
        {
            SetCurrentSave(saveFile);
            ContinueGame();
        }

any advice would be appreciated.

Could you be more specific? Which of the lines is line 23?

Hi Brian, it seems like the Null Reference was something he goes over in the next video. It was something that if you watch the video the error shows up in his Log Messages.

if (savingWraper == null) return; was all you had to do to fix it before the Foreach loop which was line 23.
foreach (string save in savingWraper.ListSave())

Also fixed my other bug about the continue not working but that was due to having !File.Exists(path). Missed deleting the ! which caused all those issues.

I’m glad you were able to get that sorted.

The only thing that concerns me is that savingWrapper shoudln’t be null. Our PersistentObjectSpawner should place one within the scene long before the SaveLoadUI gets to OnEnable()…

2 Likes

I agree that it is strange that it should be in the scene before OnEnable gets called, but I tested with some log messages, and the Onenable Debug.Log calls were happening before the Debug.Log at my Savingwrapper.

It does seem all to work now and I am not getting any errors though.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms