-3

When I make the seats[] seatArray, and try to run it, I get an error

NullReferenceException was unhandled. Object Reference not set to an instance of an object.

I think it's my printinfo function at the end.

namespace WindowsFormsApplication2
{      
    public partial class Form1 : Form
    {
       Seats s = new Seats();
        //Seats[] seatArray = new Seats[14]; //HEREEE
        public Form1()
        {

            InitializeComponent();
            listBox2.Items.Add("ROW#   A   B   C   D   E   F");
            listBox2.Items.Add("  1    " + s.PrintInfo);
           // listBox2.Items.Add( seatArray[0].PrintInfo); //HEREEE

            listBox1.Items.Add("Seats Filled:");
            listBox1.Items.Add("Windows Available:");
            listBox1.Items.Add("Regular Meals:");
            listBox1.Items.Add("LowCal Meals:");
            listBox1.Items.Add("Veget Meals:");

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string test = listBox2.SelectedItem.ToString(); /
            //string[] div = Regex.Split(test, "     ");
            //textBox2.Text = div[0];
            textBox2.Text = test; //     
        }
    }

    public class Seats // 
    {
        public char A;
        public char B;
        public char C;
        public char D;
        public char E;
        public char F;

        public Seats()
        {
            A = '.';
            B = '.';
            C = '.';
            D = '.';
            E = '.';
            F = '.';
        }

        public char Aa
        {
            set { A = value; }
            get { return A; }
        }
        public char Bb
        {
            set { A = value; }
            get { return A; }
        }
        public char Cc
        {
            set { C = value; }
            get { return C; }
        }
        public char Dd
        {
            set { D = value; }
            get { return D; }
        }
        public char Ee
        {
            set { E = value; }
            get { return E; }
        }
        public char Ff
        {
            set { F = value; }
            get { return F; }
        }


        public string PrintInfo // 
        {
            get { return this.A + "    " + this.B + "    " + this.C + "    " + this.D + "    " + this.E + "    " + this.F; }


        }
    }
}
Zong
  • 6,160
  • 5
  • 32
  • 46
  • 1
    This question is a bit of a mess. What specifically are you asking. I see a mention of a `NullReferenceException` (see http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) and something about how to display properties in text boxes? – Daniel Kelley May 21 '14 at 13:37
  • I've removed irrelevant methods from your code example (blank with nothing in them). Make sure you **only include relevant information in your question** – Sayse May 21 '14 at 13:41

1 Answers1

2

When an array is created, it is filled with the default value for the given type. For reference types, the default value is null. You need to add items to the array before you can use them:

Seats[] seatArray = new Seats[14]; 
seatArray[0] = new Seat();   // or something like this
public Form1()
{

    InitializeComponent();
    listBox2.Items.Add("ROW#   A   B   C   D   E   F");
    listBox2.Items.Add("  1    " + s.PrintInfo);
    listBox2.Items.Add( seatArray[0].PrintInfo); //HEREEE

If you want to create an array of 14 uninitialized seats you can use:

Seats[] seatArray = Enumerable.Repeat(new Seat(),14).ToArray();
D Stanley
  • 149,601
  • 11
  • 178
  • 240