0

I am new to C# and after same experience with C++ (year programming in college). Recently we started C# and thing I wanted to do is simple game with printed board (so far I have working only with console applications). I started digging (I will place links to the subjects bellow).

I am using Visual Studio Ultimate 2013. The board I want to make is to be simple board of buttons (but with extra properties). So i created a class that inherits from a button and started adding some stuff (there will be more):

class Field : Button
{
    private string owner;
    private string temp_owner;
    private int level;
    private int temp_level;

    public Field() : base()
    {
        owner = "brak";
        temp_owner = owner;
        level = 0;
        temp_level = level;
    }
}

Then I looked up how normally buttons are initialized (in Form1.Designer.cs) but Visual Studio wouldn't let me write there anything so I created method build_board and placed it in Form1.cs:

public partial class main_form : Form
{
    public main_form()
    {
        InitializeComponent();
        build_board();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private System.Windows.Forms.Button [,] Board;
    private void build_board()
    {
        this.Board = new Field[10, 10];
        this.SuspendLayout();
        //
        // Board
        //
        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
            {
                this.Board[i, j].BackColor = System.Drawing.SystemColors.Control;
                this.Board[i, j].Location = new System.Drawing.Point(10 + i * 10, 10 + j * 10);
                this.Board[i, j].Name = "Field [ " + i + " , " + j + " ]";
                this.Board[i, j].Size = new System.Drawing.Size(50, 50);
                this.Board[i, j].TabIndex = 100 * i + j;
                this.Board[i, j].Text = "";
                this.Board[i, j].UseVisualStyleBackColor = false;
            }

        this.ResumeLayout(false);
    }
}

Problem is that debugger is screaming about "NullReferenceException was unhandled" in this line:

this.Board[i, j].BackColor = System.Drawing.SystemColors.Control;

I have checked some other topics and they say it may be caused by Board being not initialized but I think I did initialize it. I will appreciate any help and maybe some other tips on how to easy print an array of custom objects.

My research:

C# NullReferenceException was unhandled - Object reference not set to an instance of an object

NullReferenceException was unhandled C#

How to create an array of custom type in C#?

(also many others about printing)

EDIT:

Answer by Pazi01: In the loop there is need to add those two lines:

    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
        {
            this.Board[i, j] = new Field();
            this.Controls.Add(this.Board[i, j]);
            ...
        }
Community
  • 1
  • 1
Bielik
  • 772
  • 9
  • 23

1 Answers1

0

The Reason is that you don't initialize data in your array

        for (int i = 0; i < 10; i++)
            for (int j = 0; j < 10; j++)
            {
                this.Board[i, j] = new Field();
                this.Board[i, j].BackColor = System.Drawing.SystemColors.Control;
                this.Board[i, j].Location = new System.Drawing.Point(10 + i * 10, 10 + j * 10);
                this.Board[i, j].Name = "Field [ " + i + " , " + j + " ]";
                this.Board[i, j].Size = new System.Drawing.Size(50, 50);
                this.Board[i, j].TabIndex = 100 * i + j;
                this.Board[i, j].Text = "";
                this.Board[i, j].UseVisualStyleBackColor = false;
            }

I have inserted this:

            this.Board[i, j] = new Field();

EDIT: You also have to add the Controls to your Form

this.Controls.Add(this.Board[i, j]);
Pazi01
  • 127
  • 10
  • The initialization helped with debugging, thanks a lot. Unfortunately The with the second line i get control underlined with red and it suggests I am missing reference. I checked and I have: using System.Windows.Forms; Also checked references. – Bielik Apr 03 '15 at 12:45
  • Edit: Sorry the s was missing on Controls – Pazi01 Apr 03 '15 at 12:50
  • Thank you so much it did the job. One final question before I close topic. Is there a way for me to see the board in designer view "Form1.cs [Designer]"? It prints once I start program but can't see it. Thank you in advance. – Bielik Apr 03 '15 at 12:58
  • I think if you Change the InitialializeComponents Method, but it is not recommended to do this because erros could break the designer – Pazi01 Apr 03 '15 at 13:04
  • OK, thank you very much once again. I am closing the subject. – Bielik Apr 03 '15 at 13:06