-1

I am trying to implement a function that retrieves the records that have been checked, the function then adds them to an ArrayList and then saves the ArrayList to ViewState. Essentially, there is a button that deletes the rows selected (via checkboxes) from the table. So after the Page_Load event, I click the button to delete the selected rows but I get a nullreference exception:

An exception of type 'System.NullReferenceException' occurred 
    but was not handled in user code
Additional information: Object reference not set to an instance of an object.

here is the function:

protected void GetSelectedRecords()
{
    ArrayList arr;

    if (ViewState["SelectedRecords"] != null)

        arr = (ArrayList)ViewState["SelectedRecords"];


    else

        arr = new ArrayList();


    CheckBox chkAll = new CheckBox();

    chkAll = (CheckBox)grdUnsubscribe.FindControl("chkAll");




    for (int i = 0; i < grdUnsubscribe.Rows.Count; i++)
    {
        if (chkAll.Checked == true)
        {
            if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value))
            {
                arr.Add(grdUnsubscribe.DataKeys[i].Value);
            }
        }

        else
        {
            CheckBox chk = (CheckBox)grdUnsubscribe.Rows[i].Cells[0].FindControl("chk");

            if (chk.Checked)
            {
                if (!arr.Contains(grdUnsubscribe.DataKeys[i].Value))
                {
                    arr.Add(grdUnsubscribe.DataKeys[i].Value);
                }
            }

            else
            {
                if (arr.Contains(grdUnsubscribe.DataKeys[i].Value))
                {
                    arr.Remove(grdUnsubscribe.DataKeys[i].Value);
                }
            }
        }
    }
    ViewState["SelectedRecords"] = arr;
}

Thanks in advance

  • 1
    [Use the debugger](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) - `chkAll` or `chk` is `null`.. and its because your call to `FindControl` doesn't return what you expect. – Simon Whitehead May 08 '14 at 22:32
  • Run it in the debugger, which variable is null? – BradleyDotNET May 08 '14 at 22:32
  • 1
    I guess `grdUnsubscribe` is a GridView and `chkAll`is somewhere in a row of it or in the header/footer. You need to use `GridViewRow.FindControl("chkAll")` because the row is the NamingContainer not the GridView. If it's in the header you can use `grdUnsubscribe.HeaderRow.FindControl...`. – Tim Schmelter May 08 '14 at 22:34
  • 3
    I'd love to say: **Jon Skeet** says, but this time **Marc Gravell** says: [don't use arraylist](http://stackoverflow.com/a/725464/1698987) to begin with :) – Noctis May 08 '14 at 22:34
  • 1
    Burn ArrayList to the ground, this isn't C# 1.0 anymore! – BradleyDotNET May 08 '14 at 22:36
  • Read this http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – reggaeguitar May 08 '14 at 23:00
  • Would mark reggaeguitar as the accepted answer if I could. Forgot to mention that chkAll is indeed the null value, thank you Simon Whitehead and Tim Schmelter for the help! – user3618426 May 09 '14 at 02:20

1 Answers1

0

Without more details of the exception its difficult to say which variable is null, a good place to start would be to look at the line that throws the exception.

For example, assuming that the exception is thrown on this line:

if (ViewState["SelectedRecords"] != null)

then the problem is that ViewState is null.

jonvw
  • 101
  • 8