I am trying to build a simulation in C#. The model itself I want to adress as a struct. Inside the model are a lot of elements (I refer to them as cells, considering that elements also refer to the chemical elements in my program and that is confusing) Each cel is a struct. One of the data types inside is an array of floats.
Below is the code I wrote:
struct Model
{
//model consists of an array of cells
public Cel[] cel = new Cel[100];
public Model() { }
}
struct Cel
{
public float[] n = new float[3];
public Cel() { }
}
class Programs
{
static int Main()
{
Model model = new Model();
model.cel[1].n[1] = 0.2f;
Console.WriteLine(model.cel[1].n[1]);
//prevent console from closing
Console.ReadLine();
return 0;
}
}
When I run this, it compiles, but gives a message:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
model.cel[].n was null. at line 24
model.cel[1].n[1] = 0.2f;
I think this is a logical way to do this and would like a solution to make it work. If this is not the way to do it, could someone point me in the right direction?
Thank you for your help.