Hello Everyone,

I have three forms, Form1, Form2 and Form3. Form3 runs first and has a button that when clicked runs Form 1 and a webBrowser window. Form1 has two buttons. When one is clicked it runs Form2 and the other button calls a function in Form3. Form2 has two buttons, one runs Form3 and the other calls a function in Form3. However when I run the code I get an error 'NullReferenceException was unhandled - Object reference not set to an instance of an object' when I click one of the buttons to call a function in Form3. I have included the code for each Form below. does anyone have any ideas as to why I am obtaining a NullReferenceException?

Thanks in advance

SubProf

Form1 Code:

namespace FunctionCalls
{
    public partial class Form1 : Form
    {
        public Form3 win2;
        public Form3 act3;
        public string res1;
        public string res2;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            res1 = "onetwo";
            res2 = "threefour";
            Form2 win4 = new Form2(res1, res2, win2);
            win4.win3 = this;
            win4.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            act3.Form3Function();
           //act3 is null when code runs
        }
    }
}

Form2 code:

namespace FunctionCalls
{
    public partial class Form2 : Form
    {

        public Form1 win3;
        public Form3 fr3;
        public string ans1;
        public string ans2;

        public Form2(string res3, string res4, Form3 f3)
        {
            ans1 = res3;
            ans2 = res4;

            fr3 = f3;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            MessageBox.Show(ans1);
            fr3.webBrowser1.Navigate("http://www.bbc.co.uk");
           //fr3 is null when code runs
        }
 private void button2_Click(object sender, EventArgs e)
        {
            Form3 frm6 = new Form3(); 
            frm6.Show(this);

        }

    }
}

Form3 code:

namespace FunctionCalls
{
    public partial class Form3 : Form
    {
        public Form2 win5;

        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            Form1 frm1 = new Form1();

            frm1.Show(this);


        }

        public void Form3Function()
    {
        MessageBox.Show("Form3 Function Called From Form2");
    }

    }
}

From basics before using reference variable you should new it
and because Form1\2\3 is classes == it reference variables
Before using fr3

fr3.webBrowser1.Navigate("http://www.bbc.co.uk");

You should

fr3 = new Form3();
fr3.webBrowser1.Navigate("http://www.bbc.co.uk");

Try to correct your code.

By the way, you've 28 posts. That's enough to know you MUST use code tags, I recommend you to edit your post ASAP before getting a bad reputation or bad flag from moderators.

Yep looking at your code F3 should be null, you never created anything to fill it.

Thank you for the help.

I am unable to edit the post and therefore I cannot make the changes requested. Apologies for this. In future I will not make the same oversight.

Thanks again for the help.

SubProf

Never mind my friend it's just an advice, please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.