Sunday, 15 April 2012

c# - Null Reference exception on an array within struct array -



c# - Null Reference exception on an array within struct array -

this question has reply here:

what nullreferenceexception , how prepare it? 21 answers

i receive null reference exception @ shapes[0].damageofshape[0] = 7;. need initialization somewhere?

public struct testarraystruct { public int[] damageofshape; } class programme { static void main(string[] args) { testarraystruct[] shapes = new testarraystruct[5]; shapes[0].damageofshape[0] = 7; } }

you need initialize shapes[0].damageofshape1, value null default:

shapes[0].damageofshape = new int[4];

you in constructor well:

public struct testarraystruct { public int[] damageofshape; public testarraystruct(int size) { this.damageofshape = new int[size]; } }

however, would have instantiate struct constructor take advantage of it:

shapes[0] = new testarraystruct(4); shapes[0].damageofshape[0] = 7;

1a previous version if reply said had instantiate shapes[0], incorrect

c# arrays struct nullreferenceexception

No comments:

Post a Comment