1

I have several buttons in my web app that I want to use to add/remove from one box to another. Using the following code which has worked in the past I am receiving a null reference error

The error points to the -1; in the line for (int i = lbAppsI.Items.Count - 1; i >= 0; i--) below

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
        {
            if (lbAppsI.Items[i].Selected == true)
            {
                lbImpactedApps.Items.Add(new ListItem(lbAppsI.Items[i].Text, lbAppsI.Items     
 [i].Value + ","));
            }
      }
    }

Object reference not set to an instance of an object is the error thrown.

entire code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Configuration;

namespace LandscapeServices
{
public partial class Update : System.Web.UI.Page
{
    public string query { get; set; }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LandscapeServicesConnectionString"].ConnectionString);
    DataSet dt = new DataSet();
    string[] IApps;
    string[] SApps;
    ListBox lbImpactedApps;
    ListBox lbSupportingApps;
    ListBox lbAppsI;
    ListBox lbAppsS;
    TextBox txtPrj;
    TextBox txtArt;


    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();

    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        string ImpactedApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label10"))).Text;
        IApps = ImpactedApp.Split(',');

        string SupportingApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label11"))).Text;
        SApps = SupportingApp.Split(',');
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                lbImpactedApps = (ListBox)e.Row.FindControl("ListBox1");
                lbSupportingApps = (ListBox)e.Row.FindControl("ListBox2");
                lbAppsI = (ListBox)e.Row.FindControl("ListBox3");
                lbAppsS = (ListBox)e.Row.FindControl("ListBox4");
                txtPrj = (TextBox)e.Row.FindControl("txtProjectName");

                foreach (string lst in IApps)
                {
                    lbImpactedApps.Items.Add(lst.ToString());
                    //lbImpactedApps.Items.FindByText(lst.ToString()).Selected = true;

                }

                lbImpactedApps.DataBind();

                foreach (string lst in SApps)
                {
                    lbSupportingApps.Items.Add(lst.ToString());
                    //lbSupportingApps.Items.FindByText(lst.ToString()).Selected = true;

                }

                lbSupportingApps.DataBind();

            }
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)
        {
            if (lbAppsI.Items[i].Selected == true)
            {
                lbImpactedApps.Items.Add(new ListItem(lbAppsI.Items[i].Text, lbAppsI.Items[i].Value + ","));
            }
      }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }

    protected void Button3_Click(object sender, EventArgs e)
    {

    }

    protected void Button4_Click(object sender, EventArgs e)
    {

    }

}
}
  • what have you tried to fix this yourself? attached with a debugger? read the "What is a NullReferenceException and how do I fix it?" question? anything ...? –  Aug 07 '14 at 13:39
  • lbAppsI is probably not initialized at this point. It should be declared (otherwise you'd get a syntax error at compile-time), but make sure you've initialized it. Otherwise, `Items` will be null, and trying to get the `Count` of a null object will cause problems – Russell Uhl Aug 07 '14 at 13:41
  • To hunt this create variables for each step. One for `lbAppsI`, another for `lbAppsI.Items` and other for `lbAppsI.Items.Count`. Just see in what line it will break. Probably in the fist as @RussellUhl pointed. I think even if there are no items in `lbAppsI` its `Items` will be instantiated. – Vitor Canova Aug 07 '14 at 13:47
  • I think these answers are great. If someone could specifically explain how to do this I would be appreciative. – OnceorTwice Aug 07 '14 at 14:10

1 Answers1

0

The error is clear : the lbAppsI is decalred but not initialized .So the exception is thrown when you try to read the items property of lbAppsI object which is null

Verify this by debugging your code , put a breakpoint here

for (int i = lbAppsI.Items.Count - 1; i >= 0; i--)

Normally you will get lbAppsI=null

Lamloumi Afif
  • 8,129
  • 23
  • 79
  • 175
  • 1
    Thats a decent answer and you are correct. I am having an issue initializing the object. Is there a formalized/preferred way to do this? – OnceorTwice Aug 07 '14 at 14:05
  • @OnceorTwice post all the class implementation to tell u where and how you can do it – Lamloumi Afif Aug 07 '14 at 14:11
  • 1
    I have added it to my original post. – OnceorTwice Aug 07 '14 at 17:44
  • @OnceorTwice First at all: avoid to open connection in the load of the page because in every post back to the page you try to open it!!! just use con.Open() when you need. Besides, I don't understand why you do this `ListBox lbAppsI;` if I understood this listbox exists in design part so you need just know its name and use it in the code behind without redefinig it , in your case the `lbAppsI` is not in the view part as you think , it is a new one. just do this `this.` and the intellisense feature helps you and list all the controls' names in the view page – Lamloumi Afif Aug 07 '14 at 17:59
  • I think part of your answer got cut off. You said just do " this" and then nothing was posted there. – OnceorTwice Aug 07 '14 at 18:18
  • @OnceorTwice the last part of the comment : it is a new one. just do `this.` and the intellisense feature helps you and list all the controls' names in the view page – Lamloumi Afif Aug 07 '14 at 18:26