Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by afot2020 · Aug 18, 2020 at 06:03 AM · listnullreferenceexceptionclass

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image afot2020 · Aug 18, 2020 at 05:33 PM 0
Share

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.

avatar image xxmariofer afot2020 · Aug 18, 2020 at 06:19 PM 1
Share

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

avatar image afot2020 xxmariofer · Aug 18, 2020 at 08:38 PM 0
Share

That was it! Thank you! Do you know why it was empty?

Show more comments
avatar image
0

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
     }
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image afot2020 · Aug 18, 2020 at 08:40 PM 0
Share

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

avatar image Spip5 afot2020 · Aug 19, 2020 at 11:14 AM 0
Share

Ho, my bad I should have noticed that, for some reasons i didnt. Glad you found a solution !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges