Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
coding of form 1

                    string query = "Select * from Guaronters where Guarantor_CNIC='" + cbocnic1.Text + "-" + cbocnic2.Text + "-" + cbocnic3.Text + "'";
                   // MessageBox.Show(query);
                    SqlDataAdapter da = new SqlDataAdapter(query, cn);
                  dt = new DataTable();


                    if (dt.Rows.Count == 0)
                        MessageBox.Show("The CNIC'" + cbocnic1.Text + "-" + cbocnic2.Text + "-" + cbocnic3.Text + "'does not Exists!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      r = 0;


                   public void Displayrecord()


                       Guarantor_Search gse = new Guarantor_Search();

                        gse.txtname.Text = dt.Rows[r]["Name"].ToString();
                        gse.txtfather.Text = dt.Rows[r]["Father_name"].ToString();
                        gse.txthusband.Text = dt.Rows[r]["Husband_name"].ToString();
                        gse.dateTimePicker1.Value = Convert.ToDateTime(dt.Rows[r]["Date_of_Birth"]);
                        gse.txtmarital.Text = dt.Rows[r]["Marital_Status"].ToString();
                        gse.txtoccupation.Text = dt.Rows[r]["Occupation"].ToString();
                        gse.txtpostal.Text = dt.Rows[r]["Postal_address"].ToString();

                        string str;
                        str = dt.Rows[r]["Customer_CNIC"].ToString();
                        string[] cnic1;
                        cnic1 = str.Split('-');
                        gse.txtcustomercnic1.Text = cnic1[0];
                        gse.txtcustomercnic2.Text = cnic1[1];
                        gse.txtcustomercnic3.Text = cnic1[2];

                        string str1;
                        str1 = dt.Rows[r]["Mobile"].ToString();
                        string[] Mobile;
                        Mobile = str1.Split('-');
                        gse.txtcode.Text = Mobile[0];
                        gse.txtmobile.Text = Mobile[1];

                        string str2;
                        str2 = dt.Rows[r]["Guarantor_CNIC"].ToString();
                        string[] cnic;
                        cnic = str2.Split('-');
                        gse.txtcnic1.Text = cnic[0];
                        gse.txtcnic2.Text = cnic[1];
                        gse.txtcnic3.Text = cnic[2];

                        if (dt.Rows[r]["Gender"].ToString() == "Male")
                            gse.rdomale.Checked = true;
                            gse.rdofemale.Checked = true;



                    public void nextrecord()
                        if (r < dt.Rows.Count - 1)
                            r = r + 1;
                            MessageBox.Show("You are at Last Record");


coding of form 2

private void button2_Click(object sender, EventArgs e)

In what line?
zeshanazam 8-Dec-12 4:48am
in if condition of nextrecord function
Jibesh 10-Dec-12 17:45pm
the variable dt might have null value.

check for why dt is null and when it gets updated.

1 solution

This kind of exceptions is one of the easiest to debug and fix. If happens, when you try to dereference some variable/member of some reference type by using one of the instance member of this type. Any such operations requires the reference to be non-null; If it's a null, this exception is thrown.

You need to execute the application under debugger allowing handling exceptions during debug. It will stop your application on the line where the exception is thrown. Set a breakpoint on this line and run again. Inspect all the reference-type variables members involved in next line to see which on is null and expected to be non-null (is dereferenced in the line in question). Fix the problem: either make sure that the object is properly initialized to guarantee non-null value, or add some code to check it for null; in case of null, do something else.

Also, next time, before asking a question like this one, make sure you use the debugger and pin-point the problem.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900