Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 themightyx · Jul 12, 2015 at 08:40 PM · c#classinstanceexception

C# NullRefException accessing static class, array value

Hey Guys, This is driving me insane. I decided on using a class array to access upgrade stats instead of a list. But something so seemingly simple is throwing an error and for the life of me I can't see it.

Very simply I created a class to hold an array of another class:

 public static class GlobalVariables {
 public class statClass
 {
         public int Dmg { get; set; }
         public int FX { get; set; }
         public int Blast { get; set; }
     }
         
     public class statHolder
     {
         public statClass[] chicken = new statClass[3];
         public statClass[] sheep = new statClass[3];
         public statClass[] pig = new statClass[3];
         public statClass[] cow = new statClass[3];
         public int[] someInts = new int[5];
     }
 
 public static statHolder currentStats = new statHolder();
 

As you can see, I've created an instance of the 'statHolder' class called 'currentStats'. The 'statHolder' class contains 4 arrays of the class 'statClass', which will contain some public values (that are currently uninitialized, which is irrelevant).

Now when I try to call or access these values from another class's Start() method:

GlobalVariables.currentStats.chicken[0].Dmg = 5;

I get the good old "NullReferenceException: Object reference not set to an instance of an object". But that is a bold-faced LIE. "chicken[0]" IS an instance of statClass as specified by "chicken = new statClass[3];". I'm so angry I want to punch a baby in the face.

There's obviously a disconnect between declaring the statClass arrays and accessing them, but I am too close to the forest to see the trees. To check my sanity i created another array with the exact same type of declaration called 'someInts'. Accessing them in exactly the same way works fine:

GlobalVariables.currentStats.someInts[0] = 5;
Can anyone point out to me where my mistake is? Because right now I'm inclined to assume that C# is just being a finicky bitch. Serves me right for trying to do something the easy way I guess.

//// UPDATE ////

Thanks guys, very clear where the disconnect was now. Initial value of a reference is always null.

 public class statHolder
 {
     public statClass[] chicken = new statClass[3]{new statClass(), new statClass(), new statClass()};
     public statClass[] sheep = new statClass[3]{new statClass(), new statClass(), new statClass()};
     public statClass[] pig = new statClass[3]{new statClass(), new statClass(), new statClass()};
     public statClass[] cow = new statClass[3]{new statClass(), new statClass(), new statClass()};
 }


Comment
Add comment · Show 2
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 maccabbe · Jul 12, 2015 at 08:47 PM 2
Share

Since statClass is a class the default value is null. Therefore your code

 public statClass[] chicken = new statClass[3];

is the same as

 public statClass[] chicken = new statClass[3]{null, null, null};

and you have to actually create objects to put in the array, i.e.

 chicken = new statClass[3];
 for(int i=0; i<3; i++){
      chicken[x]=new statClass();
 }


avatar image themightyx · Jul 12, 2015 at 10:33 PM 0
Share

Ahh of course it was something stupidly simple! Thanks for taking the time to look at this. You should have written this as a answer! I guess I thought the default uninitialized value of a class was simply the class with its default values, not just an empty reference ><

So creating an array of statClass with length 3, is not the same as creating an array and filling it with 3 statClass. Very valuable info. I guess it wouldn't be wise to create a class array with a length of 10,000 if they were all trying to be initialized at the same time (a la value type).

1 Reply

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

Answer by NoseKills · Jul 12, 2015 at 08:46 PM

When you create an array of any reference type, the initial value of all the indexes is initialized as null. That's why you get the null reference error.

When you make an array of int or other value type, it will be initialized full of the default value of that variable type, 0 in the case of int. int cannot have the value null so for sure you won't get a null ref error referencing an index of an existing int[] array.

Comment
Add comment · Show 1 · 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 themightyx · Jul 12, 2015 at 10:46 PM 0
Share

Thanks for the info! I had a very serious disconnect in my $$anonymous$$d about pointer/value types. I couldn't understand why it wasn't being initialized to its default values (when it was). It's just that its default value was null, and not a class with null values.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Null reference when trying to return an object 2 Answers

Multiple Cars not working 1 Answer

Serialization - Variables won't change on original construction 1 Answer

Distribute terrain in zones 3 Answers

How to call a new instance of a class in C# 1 Answer


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