[Help] Dictionary bug

Hey! I am working on a dictionary where I store bools, and then save them to a file.
I got a bug, and despite my researches, haven’t found any solution.
If anyone can help me, Ill be glad!
(the bug is the following: “Object Reference not set to an instance of an object” and the line it gives is the one with the comment saying Error is here, however I suspect the problem is somewhere in the start.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class newText : MonoBehaviour {
 
     public static Dictionary<string, bool> playerHolder = new Dictionary<string, bool>();
 
     public void OnEnable()
     {
         if(playerHolder.Count == 0)
         {
             playerHolder.Add("yellow", true);
             playerHolder.Add("flamingo", true);
             playerHolder.Add("parrot", true);
             playerHolder.Add("pelican", true);
             playerHolder.Add("vulture", true);
         }
         Load();
     }
     public void OnDisable()
     {
         Save();
     }
 
     public static void EnablePlayer(string name)
     {
         playerHolder[name] = true;
     }
 
     //Error is here...
     public static bool hasPlayer(string name)
     {
         return playerHolder[name];
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "test.dat");
         Data data = new Data();
         data.playerSave = playerHolder;
         bf.Serialize(file, data);
         file.Close();
     }
     public void Load()
     {
         if (File.Exists(Application.persistentDataPath + "test.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "test.dat", FileMode.Open);
             Data data = (Data)bf.Deserialize(file);
             file.Close();
             playerHolder = data.playerSave;
         }
     }
 }
 
 [Serializable]
 class TestDataSave
 {
     public Dictionary<string, bool> playerSave;
 }

what is inside the variable name when it fails? I’m guessing the dictionnary does not contain a value for the name you are asking.
Debug it to see it’s value.

The NullReferenceException means that playerHolder is null. You are assigning playerHolder to point to data.playerSave, but data.playerSave has not yet been created. In your Load() method you should first check to see if the “test.dat” file exists, and if it doesn’t call your Save() method first.

      public void Load()
      {
          if (!File.Exists(Application.persistentDataPath + "test.dat"))
          {
              Save();
          }
          if (File.Exists(Application.persistentDataPath + "test.dat"))
          {
              BinaryFormatter bf = new BinaryFormatter();
              FileStream file = File.Open(Application.persistentDataPath + "test.dat", FileMode.Open);
              Data data = (Data)bf.Deserialize(file);
              file.Close();
              playerHolder = data.playerSave;
          }
      }