locked
Shared Variable Trowing a NullReferenceException RRS feed

  • Question

  • Hello,
     I'm building a program to post somethings in my own forum, but I'm using a string called generatedCode that is on Form1 and I'm using it in Form2 with this code:
    Form1 form1 = new Form1();
    public Form2()
    {
        InitializeComponent();
        Form1 form1 = ((Form1)this.Owner);
        textBox1.Text = form1.generatedCode;
    }
    And here is my code in Form1:

    private void menuItem1_Click(object sender, EventArgs e)
    {
        Check();
        Form2 form2 = new Form2();
        generatedCode = "[b]" + textBox1.Text + space + "v" + textBox2.Text + "[/b]" +
        space + freeware + "\n" + "[u]Requirements:[/u]" + space + wm2003 + wm5 + wm6 +
        wmperso + "\n" + "[u]Overview:[/u]" + space + textBox4.Text + "\n" + "[IMG]" +
        textBox5.Text + "[/IMG]" + "\n" + "[break]" + "\n" + textBox6.Text + "\n" +
        "[u]More Info:[/u]" + "\n" + "[CODE]" + textBox6.Text + "[/CODE]" + "\n" +
        "[u]Download Instructions:[/u]" + "\n" + "[URL]" + textBox7.Text + "[/URL]" +
        "\n" + mirror;
        form2.Show();
    }
    But when I click in this menu button to create the code(in Debug) I'm getting a NullReferenceException, but what I'm doing wrong and how can I solve this?
    Tuesday, November 17, 2009 7:27 PM

Answers

  • Again :). NETCF doesn't support this.Owner. 
    It always returns null because some son-of-a-nasty-motherhood was lazy enough to not implement this.

    All You can do is to pass parent form as a parameter into the second form.

    And don't initialize forms straight when You're having those as a global objects.
    Instead do it anywhere else:

    Form1 form1;
    
    public Form2(Form1 parent)
    {
        InitializeComponent();
        form1 = parent;
        textBox1.Text = form1.generatedCode;
    }


    If You'll find my answer satisfactory or helpful - mark it as answered or vote for it! Thank You.
    "If You think You know better then me, why is Your code not working, then don't waste my time at this forum. Otherwise - do as I'm suggesting."

    I'm on MSDN just like MD House in the clinic. But I'm also a human which sometimes needs to see another doctor :)
    • Proposed as answer by Mal Loth Tuesday, November 17, 2009 7:34 PM
    • Marked as answer by Nathan Campos Tuesday, November 17, 2009 7:35 PM
    Tuesday, November 17, 2009 7:34 PM