- Home /
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()};
}
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();
}
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).
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.
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
Follow this Question
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