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

Rivendare
  • 29
  • 4
  • 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. – Matthew Watson Jan 20 '17 at 08: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. – MakePeaceGreatAgain Jan 20 '17 at 08:47
  • Also see http://stackoverflow.com/questions/18849325/c-null-reference-exception – Matthew Watson Jan 20 '17 at 08: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]; – Renatas M. Jan 20 '17 at 08: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. – Rivendare Jan 20 '17 at 17:45

2 Answers2

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();
Ben Cameron
  • 4,335
  • 6
  • 51
  • 77
  • 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. – Rivendare Jan 20 '17 at 17:45
  • 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. – Ben Cameron Jan 20 '17 at 17:47
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.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • 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. – Rivendare Jan 20 '17 at 17:45