views:

93

answers:

2

Hi guys got a wired problem (well I find it a wired problem :P)

I have an order page done in asp.net c#, and which a user adds ingredients to a set of list boxes, once the user and finished adding items, they get combined to make a sandwich then added to another list box that shows the sandwiches and their ingredients. I also have a button to allow the user to remove the highlighted sandwich from the order, this all works fine until the page loads up with a query string that skips the adding of ingredients and just shows the sandwiches on a order, the listbox of sandwiches populates fine but I then get an error when the remove button is clicked which is

System.NullReferenceException: Object reference not set to an instance of an object.

on CompletedSandwiches.SelectedItem.Text

the only difference is that I pass a query string "?EDIT=1"

and then on page_load I check

        if (!Page.IsPostBack)
        {
            if ((Request.QueryString["EDIT"] != null) && (Request.QueryString["EDIT"] == "1"))
            {

                GetCurrentSandwiches();
            }

            RemoveFilling.Enabled = false;
            RemoveCondiment.Enabled = false;
            CID = int.Parse(Session["CID"].ToString());

        }

GetCurrentSandwiches does the following:

 protected void GetCurrentSandwiches()
        {
            sandwichOnOrder.Clear();

            OrderManagement order = new OrderManagement();

            List<SarnieIngredients> bob = order.GetSandwichesOnOrder(int.Parse(Session["OID"].ToString()));
            List<int> ingredientIDs = new List<int>();


            foreach (SarnieIngredients sarnie in bob)
            {
                List<Ingredients> batchOfIngredients = new List<Ingredients>();
                double amount = 0.00;
                ingredientIDs = sarnie.IngredientIDs;
                foreach (int i in ingredientIDs)
                {
                    Ingredients contents =  ingredient.GetDesiredIngredientByID(i);

                    batchOfIngredients.Add(contents);
                    amount += contents.Price;
                }

                SandwichConentent sandwich = new SandwichConentent(batchOfIngredients, amount);
                sandwichOnOrder.Add(sandwich);

                CompletedSandwiches.Items.Add(sandwich.ToString());

            } 

and my remove buttons code does

        bool found = false;
        int i = 0;

        while (i < sandwichOnOrder.Count || found == false)
        {


            SandwichConentent content = sandwichOnOrder[i];
            if (content.ToString() == CompletedSandwiches.SelectedItem.Text)
            {
                CompletedSandwiches.Items.Remove(CompletedSandwiches.SelectedItem);
                sandwichOnOrder.Remove(content);
                found = true;
            }
        }

does anyone have any idea why I am having this problem?

thanks in advance Matt

edit ~ sorry I forgot to mention I know I haven't put validation in but I check manually that there's a item selected before I pass and when looking at the listbox in debug it is apparently null even though visually it is not.

edit 2 ~ sorry again the error occurs on this line

if (content.ToString() == CompletedSandwiches.SelectedItem.Text)

and when ?EDIT=1 is passed, it is a new page call from the main page of the web app

A: 

You need one if in your remove method:

if (CompletedSandwiches.SelectedItem.Text != null)
 ...
JonH
Oh yeah sorry I forgot to put I have not put any validation in, but i do make sure that I have got an item selected on the listbox when i click remove
Matthew De'Loughry
You should debug your code and ensure this. Throw a watch statement in and add the variable (highest level) in the watch. Then expand the variable and ensure there really is text.
JonH
yes i have sorry for not mentioning that in the post but have edited now thank you for reminding me
Matthew De'Loughry
I guess I am confused now, you've debugged it and it shows a value for it yet it throws an exception, that cant be. Where is it throwing this error, or when is the exception thrown?
JonH
Sorry, no sorry when the webpage is loading the data is populated to the listbox fine and then is displayed correctly on the listbox, I then click the remove button which has a break point attached and that shows the listbox as null, thus the error shows.sorry for the confusion
Matthew De'Loughry
Can you actually put the line that throws the error that way we can see.
JonH
I have check above
Matthew De'Loughry
A: 

Where is CompletedSandwiches declared? Either you're declaring a new one in code-behind when you shouldn't (because it's defined in the aspx), or you're adding it dynamically to the page... and in this case not instantiating it?

A control which is declared outside of a repeater in the ASPX should never be null in the code behind.

Bryan