- Home /
Array of Lists null exception when adding to list.
I have made absolutely certain that none of the values used in this code where I'm getting the error are null. I've used breakpoints, I've used Debug.Log() I can not find the cause of this error for the life of me.
Full error text:
NullReferenceException: Object reference not set to an instance of an object
MapBuilder.generateStars (Int32 x) (at Assets/MapBuilder.cs:62)
MapBuilder.UniverseInit () (at Assets/MapBuilder.cs:38)
MapBuilder.Start () (at Assets/MapBuilder.cs:31)
Code: https://hastebin.com/yozozukufu.cs
A little explanation: I've got a data sheet with 45000 stars. I don't want to instantiate them all at once and thus render them all at once so I'm trying to divide the stars into chunks that can be loaded and unloaded as needed. To assign a star to a chunk I'm making an array of lists sectorsList[,,] (line 20, 30) the array is 3 dimensional because I can assign a star to a chunk by taking its position and flooring it to an int and then making that the location. so that for example stars with locations 2.6, 1.2, 3.1 and 2.1, 1.0, 3.8 would both be found in the list [2, 1, 3] however when I actually try to add the stardata to the list at the location that i've calculated (line 62) it gives me a null reference exception despite stardata not being null or any other value that it uses being null. I have confirmed this with the debugger. and with Debug.Log();
Answer by jchester07 · Nov 11, 2017 at 06:09 AM
I'm not really good at Arrays specially on multi dimension arrays. You said that you have 45,000 stars, but in line 36 you are clamping from 1 to 45,099. Should it be 1 to 45,000?
Answer by Bunny83 · Nov 11, 2017 at 10:13 AM
You created your multidimensional array but every element in that array has it's default value "null". You haven't created a single List<StarData>
. So of course you don't have a single List where you can add something to.
You may want to do something like this:
var sector = sectorsList[h, i, j];
if (sector == null)
sector = sectorsList[h, i, j] = new List<StarData>();
sector.Add(stardata);
May I ask how large "maxSecDist" is? Keep in mind that you create a multidimensional array of rank 3. So the number of elements grow with the cubic function. If it's "10" you do 20*20*20 == 8000 elements. If it's "18" you have 36*36*36 == 46656 elements. If it's "200" you would have 8 million elements where each element can hold a List<StarData>
but is initially null as mentioned. Also do you actually want to add multiple stars to the same sector? Currently you use the same coordinates for the star position and the sector position. That would mean you can only have one star per sector or they actually have the same position within the system.
Ooops, I was writing up a lengthy answer without noticing that @Bunny83 had already posted an answer... sorry...
No worries... It happens to me countless times. Since i also write rather "long" answers at the time i actually submit my answer there might already be others.
Your answer
Follow this Question
Related Questions
Null Ref Exception on a script that should have a reference in it? 2 Answers
MergeSort function acting strange 0 Answers
Custom Inspector: For every new element in one array create a brand new array? 0 Answers
Inventory String error 1 Answer
How can I create a tree like list/array structure? 0 Answers