0

When I try remove item of a list I get "Object reference not set to an instance of an object". Here is the code:

protected void btnIzvadi_Click(object sender, EventArgs e)
{
    if (Session["kosnice"] == null)
    {
        lblError.Text = "Вашата купувачка кошничка е празна";
    }
    else
    {
        try
        {
            List<string> kosnicka = new List<string>();
            kosnicka = (List<string>)Session["kosnice"];
            for (int i = 0; i < lstKosnicka.Items.Count; i++)
            {
                kosnicka.Add(lstKosnicka.Items[i].Text);
            }
            string pom = lstKosnicka.SelectedItem.Text;
            kosnicka.Remove(pom);
            for (int i = 0; i < kosnicka.Count; i++)
            {
                lblError.Text = " " + kosnicka[i];
            }
            Session["kosnice"] = kosnicka;
            lstKosnicka.DataSource = kosnicka;
            lstKosnicka.DataBind();
        }
        catch (NullReferenceException err)
        {
            lblError.Text = err.Message;
        }
    }


}

The list form that I get the exception is not empty and it has reqired field validator concerned to btnIzvadi.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Katerina
  • 364
  • 6
  • 22
  • which line is the exception on? – Preet Sangha Jul 20 '12 at 09:13
  • Note that the two first lines of the try block should be merged into List kosnicka = (List)Session["kosnice"]; – chmike Jul 20 '12 at 09:29
  • Could this be the instruction causing the problem ? Session["kosnice"] = kosnicka; Might need a cast. – chmike Jul 20 '12 at 09:31
  • @PreetSangha the exception is in string pom = lstKosnicka.SelectedItem.Text; – Katerina Jul 20 '12 at 09:35
  • @chmike I merge the 2 lines and I get the exception again on the same line `string pom = lstKosnicka.SelectedItem.Text;` – Katerina Jul 20 '12 at 09:38
  • I get the nullreference exception not the null pointer, sorry for the mess :) – Katerina Jul 20 '12 at 09:41
  • Is lstKosnicka.SelectedItem null? – Bob Vale Jul 20 '12 at 09:43
  • No, it is not null I have another button which "click" action adds elements into lstKosnicka – Katerina Jul 20 '12 at 09:45
  • You have another button that adds elements into lstKosnicka, but do you have anything that requires the user to choose anything in the list before this triggers? To help debug it, you can add an if(lstKosnicka.SelectedItem != null) before string pom = ... If lstKosnicka.SelectedItem is throwing a null reference exception, then it tells me that either nothing is chosen, or the EnableViewState = "false" on the asp page. Just an idea. :) – Gobbledigook Jul 20 '12 at 09:55
  • @Gobbledigook Like I wrote I fill lstKosnicka, but I do the following ` if (lstKosnicka.SelectedItem == null) { lblError.Text = "item is null"; }` and I get that message. How is that possible. I select item from lstKosnicka first then I click the button – Katerina Jul 20 '12 at 09:56
  • I have requiredfiled validation for all ListBoxes – Katerina Jul 20 '12 at 10:02
  • 1
    @Katerina In your page_load (page_init, page_prerender, etc.) do you reset lstKosnicka? Remember that the page does a full postback whenever a control is actioned upon. So if in your Page_Load you set lstKosnicka.SelectedItem = null (for instance), when you click the button, it will go through the Page_Load and reset it before the btnClick method runs. – Gobbledigook Jul 20 '12 at 10:28
  • @Gobbledigook I have this in Page_Load `protected void Page_Load(object sender, EventArgs e) { List kosnicka; if (Session["kosnice"] == null) { kosnicka = new List(); } else { kosnicka = (List)Session["kosnice"]; lstKosnicka.DataSource = kosnicka; lstKosnicka.DataBind(); lstKosnicka.Visible = true; btnDodadi.Visible = true; btnIzvadi.Visible = true; } }` – Katerina Jul 20 '12 at 10:31
  • @Gobbledigook thank you. After I copy this to you I notice that I forgot IsPostBack method :). THAT IS ALL ABOUT :D – Katerina Jul 20 '12 at 10:36
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – John Saunders May 15 '14 at 02:36

1 Answers1

0

Instead of

    string pom = lstKosnicka.SelectedItem.Text;

Try this:

    string pom = lstKosnicka.Text;
Amit Mittal
  • 1,129
  • 11
  • 30