- Home /
The question is answered, right answer was accepted
Error message when accessing class array
I have a class called road, as you can see below :
public class Road {
public int typeIndex;
public int[] intersection;
public Bezier[] spline;
}
I create an array of this class, using :
Road[] outputRoad = new Road[1];
Within that class, you can see a variable called Bezier[], another array of a custom class. This class is :
public class Bezier {
public Vector3[] controlPoint;
public float length;
public BoundingBox boundary;
}
And within that is an array of Vector3[] called controlPoints. Within my code, I need to set controlPoints, however, as expected the code throws up the error
NullReferenceException: Object reference not set to an instance of an object
System.Array.Resize[Bezier] (.Bezier[]& array, Int32 newSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1913)
roadTool_cs.BuildRoad (.buildInfo roadInfo) (at Assets/toolManager/roadTool_cs.cs:170)
roadTool_cs.Update () (at Assets/toolManager/roadTool_cs.cs:84)
Since I haven't allocated memory to the arrays. Now I tried doing this :
System.Array.Resize (ref outputRoad, 1);
System.Array.Resize (ref outputRoad[0].spline, 1);
System.Array.Resize (ref outputRoad[0].spline[0].controlPoint, 4);
To set the size of the arrays, however on the second line, I get the exact same error as the one above, and I have no idea why. I set the size of every array from the road class, the Bezier class, and also the array of Vector3's. What am I doing wrong? I had an idea, to put a method within the class, and write something like:
outputRoad = new Road[1];
but I STILL get an error. Anyway thanks for your time, hope you can help. :)
Your array of of controlPoint have the size 0, but this should throw you a Index out of range, in case you tried to assing something to it. And your Bezier array is also size 0
Your Road array is a nullRef, because in this code you posted, you never actualy put a road in the array, you just allocate a Road array of size 1.
But i can't say much because i'm solo based on what you have posted here, i don't know if you fill this arrays latter, but in the state they are, they won't work.
I create a new Road variable higher up in the script, but at that point in time, the array wouldve been 0 in length. Youre correct, i need to create an instance of it afterwards. Ill see if that works.
Answer by Arkaid · Jul 15, 2016 at 12:46 AM
You're not initializing any array and you can't resize a null. Do this first:
public class Road {
public int typeIndex;
public int[] intersection = new int[1];
public Bezier[] spline = new Bezier[1];
}
public class Bezier {
public Vector3[] controlPoint = new Vector3[1];
public float length;
public BoundingBox boundary;
}
Then you can probably Resize.
That said, if you're going to be resizing arrays, I'd recommend using List instead.
Can i write it in a method within the class? So a method called Public Road () {} and initialise them in there ins$$anonymous$$d?
Sure, you could initialize the arrays in the constructor :)
Also keep in $$anonymous$$d that this line:
outputRoad = new Road[1];
only creates a Road array with just one element, but this element is null. You haven't yet created a Road object
// create Road object and store it as first element in the array.
outputRoad[0] = new Road();
Well this is my section of code :
Road[] outputRoad = new Road[1];
outputRoad[0] = new Road ();
outputRoad[0].spline [0].controlPoint [0] = roadInfo.userPoint [0];
outputRoad[0].spline[0].controlPoint[3] = roadInfo.userPoint[2];
And these are my classes :
public class Bezier {
public Vector3[] controlPoint = new Vector3[4];
public float length;
public BoundingBox boundary;
}
public class Road {
public int typeIndex;
public int[] intersection = new int[0];
public Bezier[] spline = new Bezier[1];
}
Yet this still isn't working. Same error,
I added the line : outputRoad[0].spline[0] = new Bezier(); And everything works now. Thing is I've done some similar stuff higher up on my project and I've never needed to do this, write " = new type;" whenever accessing the class within the class. And that's because I initialised it inside the constructor, and forgot to do that with these classes. :)
Why do we have to do this, may I ask? If I create an integer variable, I don't have to write something like
int randomNum = new integer;
because, if it has value, it's automatically null. And then if I try to set it, it doesn't throw back a random error. Why doesn't unity just automatically initialise the custom class to all default values?
That's not a Unity issue, that's C#
You need to understand the difference between a value type and a reference type.
As their name indicate, value types hold a value. int and float are good examples.
Reference type hold not a value but a reference to the actual data. Classes fall in this category.
When you create a reference, you have a variable that can point to where the data is, but until you assign it to something, it points to nothing, that is, you hold a null reference.
Value types on the other hand directly hold a value, so there's no need for a 'new' call
It helps a bit if understand how computer memory works. You should read up on values, references, stack, heap and pointers and you'll understand why the difference :3
I know a fair deal, the reference is most likely an address to a memory location and all that stuff xD I didn't know that there were references/values, I understand now. Anyway not it's relevant but I got my program finished and works, thanks for your help. :)
Follow this Question
Related Questions
Custom Class Array Serialization Problem 0 Answers
WARNING : Assignment to temporary. 1 Answer
Add custom declared variables in a Array 1 Answer
C# custom class array error in adding a new entry. 1 Answer
Array of abstract class in inspector 2 Answers