0

I Have 2 form,what i'm trying to do is get a value from a TextBox on form1 then assign it into other variable in form2 so i could re-use it in form2 without re-calling it from form1

Form1 (It's a login form)

public void LoginBttn_Click(object sender, EventArgs e)
    {
        cnnctString = "SERVER=" + server + ";" + "DATABASE=" + DB + ";" + "UID=" + uid + ";" + "PASSWORD=" + pwd + ";";

        connection = new MySqlConnection(cnnctString); //The one that i want to fetch for the 2nd form

        if (OpenConnection() == true)
        {
            Form2 menu = new Form2();
            menu.Visible = true;
            this.Visible = false;
        }

    }

In the form1 i have initialize server, DB, UID, and pwd (each of them have value, pwd and UID value are fetched from Textbox ) and the OpenConnection also return true.

and then on

Form2

public Form1 oF;

public MySqlConnection NTD()
    {
        MySqlConnection F = oF.connection;//It's Highlighted in this line.
        return F;
    }

public MySqlCommand cmd = new MySqlCommand();

public void A9_Click(object sender, EventArgs e)
    {
         A9.Text = "X";
         string query = "UPDATE orders SET Client=" + Name + " , Stat='1' WHERE No='A9'";
         cmd.CommandText = query;
         cmd.Connection = NTD();
         cmd.ExecuteNonQuery();

    }

name value is fetched from a Textbox on the same form

There's no compile error and other before-compile error

But When i succesfully run it and click button A9 it's throw NullReferenceException

I'm not sure that connection is null since i can sucessfully login and show the form2.

Am i miss something ? any idea about this ?

I'm a novice and don't know what wrong.And if you could,please describe it as easy as possibble if it's require something advance.

Thankyou.

Tommy Aria Pradana
  • 574
  • 1
  • 4
  • 18
  • You can find what's wrong with very few steps: add a breakpoint and **debug your code**. You'll see which variable is null (and why). BTW Why +1? a tour with debugger is pretty required before posting a question... – Adriano Repetti Apr 13 '14 at 16:15

1 Answers1

4

You seem to have forgotten to set the oF variable to the instance of Form1. This results in oF being null, so an attempt to access the connection member variable throws an exception. One way to solve this issue is to set it in the constructor:

public Form2(Form1 parentForm)
{
    if(parentForm == null)
        throw new ArgumentNullException("parentForm");
    this.oF = parentForm;
}

//...

Form2 menu = new Form2(this);

You can debug this issue by setting a break point to the highlighted line (the one you mentioned) and check what's null.

nikeee
  • 10,248
  • 7
  • 40
  • 66
  • Thanks it's work,but when i put the **if** function on it it's always throw null on itself.But if i not put it,it's work like a charm. Any idea how this could happen ? Thanks – Tommy Aria Pradana Apr 16 '14 at 03:19
  • Of course. There was a mistyping. It should be `If(parentForm == null)`, not `if(of == null)`, because `oF` is always `null` before assigning it. – nikeee Apr 16 '14 at 20:11
  • So i better use the **if** function or not ? because i didn't use it and not found any problem (yet) – Tommy Aria Pradana Apr 18 '14 at 06:17
  • It isn't necessary, but it's highly recommended if you have publicly accessible APIs. It's called parameter input validation. – nikeee Apr 19 '14 at 13:14