- Home /
Integer List values don't get initialized in Constructor of Class
I created a List but get a NullReferenceException whenever I try to refer to it. I have a DataManager Class (saves & loads Data) and a UserData Class (stores the fields that are needed to be saved or loaded). In the User Data Class I declare in initialize a List of integer type. The List contains the unlocked Levels and it is inititialized in the Constructor of the User Data Class.
This is the DataManager Class:
public static class DataManager
{
public static List<int> GetUnlockedLevels()
{
UserData userData = Load();
return userData.unlockedLevels; // This method returns nothing, not even null!
}
private static void Save(UserData data)
{
string path = GetDataFilePath();
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = File.Open(path, FileMode.OpenOrCreate))
{
binaryFormatter.Serialize(fileStream, data);
}
}
private static UserData Load()
{
string path = GetDataFilePath();
if (!File.Exists(path))
{
UserData userData = new UserData();
Save(userData);
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (FileStream fileStream = File.Open(path, FileMode.Open))
{
return (UserData)binaryFormatter.Deserialize(fileStream);
}
}
And here comes the UserData Class:
[Serializable]
public class UserData
{
public int score;
public List<int> unlockedLevels;
public UserData()
{
score = 50;
unlockedLevels = new List<int>();
unlockedLevels.Add(1); //unlocked by default
unlockedLevels.Add(2); //unlocked by default
unlockedLevels.Add(3); //unlocked by default
}
}
The problem is: the first method of the DataManager "GetUnlockedLevels()" returns nothing.
The weird part: I have the exact same Data Manager in another project where it works properly. In that other project, the GetUnlockedLevels-method returns "System.Collections.Generic.List´1[System.Int32]" when I return it via "Debug.Log". But in the new project, the method returns literally nothing (not even null; the exception comes at a later point) I am sure that I didn't make a copy-paste-mistake. What could be the root for this error?
Answer by xxmariofer · Aug 18, 2020 at 07:35 AM
can you add this line in the Load method and tell me what it prints?
Debug.Log(File.ReadAllText(path));
my bet is that the file exists but is empty somehow
I put the line right after the line "BinaryFormatter binaryFormatter = new BinaryFormatter();" and it prints nothing (literally like " "). $$anonymous$$ay that be the cause? But why are the other variables working, e. g. when I want to print "score"? When I change the return line in the "GetUnlockedLevels()"-method to
return userData.score;
then it returns the given value which is 0.
yes then is because it is empty, default int value is 0 they dont need to be initialize, if you dont give them a value it will have default value 0, list are not the same, they are objects, and like all objects need to get initialize, so if they dont it is simply null value, thats the reason, my advice is to manually delete that file if posible and test again, ill change it to an answer so you can confirm if it worked my sugestion
That was it! Thank you! Do you know why it was empty?
Answer by Spip5 · Aug 18, 2020 at 06:05 PM
There is something weird. When I copy-paste your code, it tells me the unlockedlevels from parameters and unlockedlevels from constructor is not the same (not clue why). A copy paste from parameter to constructor fixes it. I don't get why.
this works here :
[System.Serializable]
public class UserData
{
public int score;
public List<int> unlockedlevels = new List<int>();
public UserData()
{
score = 50;
unlockedlevels.Add(1); //unlocked by default
unlockedlevels.Add(2); //unlocked by default
unlockedlevels.Add(3); //unlocked by default
}
}
Thats because I made a copy-paste-mistake. At first I wrote "unlockedlevels" and then "unlockedLevels" (small L vs big L). I am sorry for the confusion. That happened only when I prepared the post above. I corrected it
Ho, my bad I should have noticed that, for some reasons i didnt. Glad you found a solution !
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How can i store the Amount of an item i have in an inventory? 0 Answers
How do I Create 5 Random Racers? 2 Answers
Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer
My Project doesn't work - Generic.List throws NullReferenceException ?! 0 Answers