hi i wrote a code i c# and i create stuct array from struct that has a array but i have a error on Visual Studio.
my code:
public
class o { public int[] a = new int[5]; public int[] b = new int[5]; public int[] c = new int[5];
}
public class b { public o[] yy = new o[5];
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
b[] oo = new b[5];oo[0].yy[0].a[1] = 4;
}
}
i have error:
Object reference not set to an instance of an object.
how i can solution this problem ?
best regard.
Jan MontanoPosted Apr 7, 2009, 10:05 PM
oo[0] is null, that is why you were encountering an "Object reference not set to an instance of an object" error. You need to initialize your class arrays first before usage.
class b
{
public o[] yy = new o[5];
public b()
{
// initialize the array variables
for (int index=0; index
yy[index] = new o();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
b[] oo = new b[5];
// always remember that you have to initialize your class array before you'll be able to use it
for (int index = 0; index < oo.Length; index++)
{
oo[index] = new b();
}
oo[0].yy[0].a[1] = 4;
}