Creating objects dynamically

In a UI app I’ve build, I create a label like this:

    private void InitializeCurrentPerlLabel() {
        currentPerlLabel = new System.Windows.Forms.Label();
        SuspendLayout();

        currentPerlLabel.AutoSize = true;
        currentPerlLabel.Location = new System.Drawing.Point(10, 10);
        currentPerlLabel.Name = "currentPerlLabel";
        currentPerlLabel.Size = new System.Drawing.Size(35, 35);
        currentPerlLabel.TabIndex = 0;
        currentPerlLabel.Font = new Font(Font, FontStyle.Bold);

        Controls.Add(currentPerlLabel);
        Name = "BBUI";
        ResumeLayout(false);
        PerformLayout();
    }

…and I call it simply like this in the constructor: InitializeCurrentPerlLabel().

Now, I’m at the point where I want to automate the creation of various elements. To do so, I will have all the data in a JSON file, will loop over the elements and create them with a build method.

To test, I’ve got a basic incarnation of the method I’ll eventually use to do this, but at this time, I’m just testing a single label. I’ve changed my code to this:

    private void BuildLabel(string name, Label label) {
        var data = Conf[name];
        label = new System.Windows.Forms.Label();

        SuspendLayout();

        label.AutoSize = data["autosize"];
        label.Location = new System.Drawing.Point(10, 10);
        label.Name = name;
        label.Size = new System.Drawing.Size(35, 35);
        label.TabIndex = 0;
        label.Font = new Font(Font, FontStyle.Bold);

        Controls.Add(label);
        Name = data["name"];
        ResumeLayout(false);
        PerformLayout();       
    }

…and call it like this: BuildLabel("currentPerlLabel", currentPerlLabel);.

It compiles fine, however, I get a System.NullReferenceException: Object reference not set to an instance of an object. error at runtime.

I’m not a fluent C# coder, so could I please get some help as to how I go about setting up my pre-declared currentPerlLabel Label object (through what I would call a reference) in the manner I’m hoping to?

Stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at BBUI.CurrentPerlLabel_Redraw()
   at BBUI.DrawComponents()
   at BBUI.Form1_Load(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

>Solution :

Simply add a ref keyword on your label. This means thats the references can be changed, as you are creating a new one.

private void BuildLabel(string name, ref Label label) {
        var data = Conf[name];
        label = new System.Windows.Forms.Label();

        SuspendLayout();

        label.AutoSize = data["autosize"];
        label.Location = new System.Drawing.Point(10, 10);
        label.Name = name;
        label.Size = new System.Drawing.Size(35, 35);
        label.TabIndex = 0;
        label.Font = new Font(Font, FontStyle.Bold);

        Controls.Add(label);
        Name = data["name"];
        ResumeLayout(false);
        PerformLayout();       
    }

Usage :

BuildLabel("currentPerlLabel", ref currentPerlLabel);

Leave a Reply