0

I am creating a construct from my own class:

namespace testproject
{
    public class frameSructure
    {
        public string type = "n/a";
        public string reader = "n/a";
    }
}

When I am using it in a form as a single construct everything is fine but when I am creating as an array, When I try to use it:

namespace testproject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public frameSructure[] frame = new frameSructure[10];
        private void Form1_Load(object sender, EventArgs e)
        {
            frame[1].type = "n/a";
        }
    }
}

I get a system null reference exception. {"Object reference not set to an instance of an object."} at

frame[1].type = "n/a"; 

Can someone help me understand why? Thank you

5
  • 1
    When you create an array of reference types (such as your frameStructure) the array is filled with nulls; it's not filled with default-constructed instances of that class. You will have to loop filling it with actual instances. Commented Jan 20, 2017 at 8:47
  • This is because new frameStruct only allocates memory on the stack/heap, but does not create any instance of that struct. You need frame[1] = new frameStruct() beforehand. Commented Jan 20, 2017 at 8:47
  • Also see stackoverflow.com/questions/18849325/c-null-reference-exception Commented Jan 20, 2017 at 8:48
  • Note: array index starts from 0. If you declare size of 10, index boundaries will be 0-9. So the first item in your array is frame[0]; Commented Jan 20, 2017 at 8:50
  • Thank you for the correction. I dont understand why do I need to create all the elements of the array one by one if I have declared the array made of the class but I know how to use it now. Thank you. Commented Jan 20, 2017 at 17:45

2 Answers 2

0

You have initialized the array, but not the values inside it. This means that while the array does have 10 entries, they all are null.

Change your form load to this:

    private void Form1_Load(object sender, EventArgs e)
    {
        frame[1] = new frameSructure();
        frame[1].type = "n/a";
    }

btw, arrays in c# starts with 0, not 1.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the correction. I dont understand why do I need to create all the elements of the array one by one if I have declared the array made of the class but I know how to use it now. Thank you.
0

You would need to add an item to the array first. In your code you have only created an empty array of the framestructure type.

You would add items to your array like so...

frame[1]= new frameSructure();

2 Comments

Thank you for the correction. I dont understand why do I need to create all the elements of the array one by one if I have declared the array made of the class but I know how to use it now. Thank you.
You have created the data structure (the array), but haven't put any data in it. Think of the array like a bus and the people in it as the data. At the moment you have an empty bus.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.