Question Array and struct

johnix

New member
Joined
Oct 30, 2014
Messages
4
Programming Experience
3-5
hi,
i've this code

C#:
public class a
{
    sa sa1 = new sa(0);
        
    public void test ()
    {
        uint i, j; 

        for (i = 0; i <= 1; i += 1) {
            for (j = 0; j <= 13; j += 1) {
                sa1.va[i].vb[j] = false;
            }
        }
    }
        
    public struct sa
    {
        public sb[] va;
    
        public sa(uint i): this()
        {
            va = new sb[2];
        }
    }

    public struct sb
    {
        public bool[] vb;

        public sb(uint i): this()
        {
            vb = new bool[14];
        }
    }
}


When i try to run void test i receive this error: ...Object reference not set to an instance of an object"
where am I doing wrong?
 
Last edited by a moderator:
In your `sa` constructor, you create the array by specifying a length but you never create `sb` objects to assign to the elements. That means that the `sb` constructor that you wrote never gets invoked and therefore the `vb` array in each `sb` object is never set, therefore it's null for each element. Most likely you shouldn't be using structures at all but rather classes.

To prove it to yourself, place a breakpoint on each structure constructor using the F9 key. When you run the code, you'll see one of them get hit but the other one not.
 

Latest posts

Back
Top Bottom