- Home /
[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;
}
Answer by jmorhart · Nov 04, 2015 at 11:05 PM
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;
}
}
I may be mistaken, but actually my code was kinda wrong. What I want to do, is to create the dictionary once, and then save it and load it back etc. public void OnEnable() { Load(); if (playerHolder != null) { playerHolder = new Dictionary (); playerHolder.Add("yellow", true); playerHolder.Add("fla$$anonymous$$go", true); playerHolder.Add("parrot", true); playerHolder.Add("pelican", true); playerHolder.Add("vulture", true); } Debug.Log(playerHolder); }
This shows better what I want. However now I am not able to save the dicionary at the end of the game to the dictionary inside the Serializable Class.
I think I fixed it with this code, sorry for the wrong formatting :P Still Ill accept your answer, since its the only one! Thanks for the help.
Answer by candy_man · Nov 05, 2015 at 01:04 PM
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.