0

I have this code

public List<List<decimal>> connection = new List<List<decimal>>();

Calling connection.Count() give error NullReferenceException , why ? i already initialize it with empty list ? enter image description here

Here is the full code , pretty straightforward

public List<Neuron> neuron = new List<Neuron>(new Neuron[480501]);
void LateUpdate()
{
Debug.Log("nc : " + neuron[0]); // runs fine
Debug.Log("nc : " + neuron[0].connection); //error
}
public class Neuron
{
public List<List<decimal>> connection = new List<List<decimal>>();
}
Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
  • What do your logs print? You are creating a list from the array `new Neuron[480501]` which has `480501` entries of type `Neuron` that are **all `null`**! Your first log works but should print `nc : null` or at least the entry `neuron[0]` **is** `null` .. then you get an exception because you can't access `connection` of an item that is `null` ... – derHugo Jan 12 '21 at 08:26

0 Answers0