简体   繁体   中英

Null reference when switching between forms

I have a program that has a main form with a DevExpress XtraGrid control. It has many rows with data from my DB. I have an Edit form and an edit button on the main form to edit the selected row. I was able to pull the info for the selected object into the Edit form fine, but for some reason I am having trouble doing it again when I run the UPDATE command. I'm referring to the selected row on my main form as gridView1.GetFocusedRow() and this worked perfectly for my showAttributes method, but no longer works.

My code follows. It is returning an exception because 'call' is null. Just to note a few things: I've tried doing main.gridView1.Focus() and main.gridView1.FocusRowHandle(0) if I'm just editing the first row - neither fixed the problem. This seems to tell me that the row is still focused correctly but the reference was lost somehow.

In Main.cs

    private void btnEdit_Click(object sender, EventArgs e)
    {
        //This is the key, that lets you access an attribute of the selected row. 
        Object obj = gridView1.GetFocusedRow();

        //1) Show NewEdit Form
        Edit edit = new Edit();
        edit.Show();

        //2) Display properties of this object from DB
        edit.showAttributes(obj);
    }

In Edit.cs:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        Main main = new Main();
        Object obj = main.gridView1.GetFocusedRow();
        try
        {
            performUpdate(obj);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
    }

    public void performUpdate(Object call1)
    {
        Main main = new Main();
        CallLog call = new CallLog();
        call = call1 as CallLog;

        executeSQLUpdate(call.Oid);
        main.xpServerCollectionSource1.Reload();
    }

Here is the showAttributes code - does the same thing but works

    public void showAttributes(Object call1)
    {
        try
        {
            Main main = new Main();
            CallLog call = new CallLog();
            call = call1 as CallLog;

            txtCompany.Text = call.CompanyName;
            txtFirst.Text = call.FirstName;
            txtMiddle.Text = call.MiddleName;
            txtLast.Text = call.LastName;
            txtPhone.Text = call.PhoneNumber;
            txtFax.Text = call.Fax;
            txtEmail.Text = call.Email;
            txtAttention.Text = call.Attention;
            txtCareOf.Text = call.CareOf;
            txtAddress1.Text = call.Address1;
            txtAddress2.Text = call.Address2;
            txtCity.Text = call.City;
            txtState.Text = call.State;
            txtZip.Text = call.ZipCode;
            txtProvince.Text = call.Province;
            txtCountry.Text = call.Country;
            txtMessage.Text = call.Message;
            txtResponse.Text = call.Response;

            if (call.VIP == 1) { chkVIP.Checked = true; } else { chkVIP.Checked = false; }
            if (call.ThreatCall == 1) { chkThreat.Checked = true; } else { chkThreat.Checked = false; }
            if (call.FollowUp == 1) { chkFollowUp.Checked = true; } else { chkFollowUp.Checked = false; }
            if (call.EscalationRequired == 1) { chkEscalation.Checked = true; } else { chkEscalation.Checked = false; }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
            return;
        }
    }

Also... I have tried doing this several other ways, without parameters, in different locations, etc. The problem is main.gridView1.GetFocusedRow() returns null. Also, running it as a series of methods from the main form does not work either ( gridView1.GetFocusedRow() is also null).

In Edit.cs you do

    Main main = new Main(); 
    Object obj = main.gridView1.GetFocusedRow(); 

this will create a new Main without displaying it and then try to get an obj from a gridView obviously emtpy. The same error is repeated in all the code shown.
Also, it's very confused the creation and use of CallLog instances.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM