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. :slight_smile:

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.

1 Answer

1

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 instead?

Also keep in mind 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();

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?

I believe protected members are not serialised. You could add a [SerialiseField] Attribute to solve this.