Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I get the error message Object reference not set to an instance of an object on this line.

var query = context.CreateQuery<customer>("GetProducts").AddQueryOption("selectedCategory", selectedCategory);


This is the code..

C#
ContentControl control;
            string selectedItem = string.Empty;
            int selectedCategory = 0;
            string selectedPrice = string.Empty;

            // Controls for Category ComboBox
            control = categoryComboBox.SelectedItem as ContentControl;
            if (control != null)
            {
                selectedItem = (string)control.Content;
            }

            // Controls for Price ComboBox
            control = priceComboBox.SelectedItem as ContentControl;
            if (control != null)
            {
                selectedPrice = (string)control.Content;
            }

            #region
            if (selectedItem == "Electronics and Computers")            // Entertainment Option
            {
                selectedCategory = 1;
            }
            else if (selectedItem == "TV and Video")            // Entertainment Option
            {
                selectedCategory = 2;
            }
            else if (selectedItem == "Home Audio and Theater")            // Entertainment Option
            {
                selectedCategory = 3;
            }
            else if (selectedItem == "Camera, Photo and Video")            // Entertainment Option
            {
                selectedCategory = 4;
            }
            else if (selectedItem == "Cell Phones and Accessories")            // Entertainment Option
            {
                selectedCategory = 5;
            }
            else if (selectedItem == "Video Games")            // Entertainment Option
            {
                selectedCategory = 6;
            }
            else if (selectedItem == "MP3 Players and Portable Speakers")            // Entertainment Option
            {
                selectedCategory = 7;
            }
            else if (selectedItem == "Car Electronics and GPS")            // Entertainment Option
            {
                selectedCategory = 8;
            }
            else if (selectedItem == "Musical Instruments")            // Entertainment Option
            {
                selectedCategory = 9;
            }
            else if (selectedItem == "Electronics Accessories")            // Entertainment Option
            {
                selectedCategory = 10;
            }
            else if (selectedItem == "Laptops and Tablets")            // Entertainment Option
            {
                selectedCategory = 11;
            }
            else if (selectedItem == "Desktops & Monitors")            // Entertainment Option
            {
                selectedCategory = 12;
            }
            else if (selectedItem == "Computer Accessories and Peripherals")            // Entertainment Option
            {
                selectedCategory = 13;
            }
            else if (selectedItem == "Computer Parts and Components")            // Entertainment Option
            {
                selectedCategory = 14;
            }
            else if (selectedItem == "Software")            // Entertainment Option
            {
                selectedCategory = 15;
            }
            else if (selectedItem == "PC Games")            // Entertainment Option
            {
                selectedCategory = 16;
            }
            else if (selectedItem == "Musical Instruments")            // Entertainment Option
            {
                selectedCategory = 9;
            }
            #endregion



            var query = context.CreateQuery<Customer>("GetProducts").AddQueryOption("selectedCategory", selectedCategory);
                                                                //    .AddQueryOption("selectedPrice", selectedPrice);

            productsViewSource.Source = query.Expand("ProductOptions");

            productsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, "ProductOptions");


and heres the query..


C#
[WebGet]
        public IQueryable<Product> GetProducts(int selectedCategory)
        {
            if (selectedCategory > 0)
            {
                iShopEntities context = this.CurrentDataSource;
                var products = from product in context.Products    // .Include("ProductOptions")
                               where product.CategoryID == selectedCategory
                           //    && product.Price == selectedPrice
                               select product;

                return products;
            }
            else
            {
                throw new DataServiceException("Blah");
            }
        }
Posted
Updated 24-Apr-14 15:41pm
v2
Comments
[no name] 24-Apr-14 20:30pm    
Okay since your "Object reference not set to an instance of an object" would be exactly the same as your previous occurrences of that error message, what is your question?
[no name] 24-Apr-14 20:33pm    
I basically want to know how to get rid of it. Like im not sure what it means and where it applies to. Like i need some help on how to fix it.
[no name] 24-Apr-14 20:37pm    
http://www.codeproject.com/Answers/733413/Object-reference-not-set-to-an-instance-of-an-o#answer2
Sergey Alexandrovich Kryukov 24-Apr-14 21:40pm    
Thank you, Wes.

Ah, this is my answer to Chris. So, not only this is a re-post, but my answer was majorly ignored. OP's comments were unrelated to this text. I have no idea how to help in such situations.

Chris, this post should be removed, as re-posts are considered as abuse. If you still have problems, you need to comment on existing answers in order to ask some follow-up questions.

—SA
[no name] 25-Apr-14 11:29am    
I do apologize. I will admit the first time you posted the reply to my previous question. I did ignore it fully as I was on a strict time limit and wanted to do as little reading as I could. However now I can be more lax with my time, I have read your reply and it was most enlightening and in fact helped me solve the problem.

Sorry once again, and thank you :)

1 solution

I have solved it and saw that the query line did not have a value. But thanks to SA for his reply on a previous question that helped solve the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900