Adding items in order gives NullReferenceException (WooCommerce, in C# and using WooCommerce.NET addin.

I am trying to use this code for making an order in Woocommerce, using this add-on :https://github.com/XiaoFaye/WooCommerce.NET. My code is this:  

        var order = new Order() { status = "pending" };
        order.currency = "EUR";
        order.line_items.Add(new OrderLineItem()
        {
            id=44,
            product_id = 44,
            quantity=2
        }
          );
        order.customer_id = 1;
 
       Task ord = wc.Order.Add(order);
       ord.Wait();
But everytime it gives me this exception:
System.NullReferenceException: 'Object reference not set to an instance of an object.'

I have tried everything but no success...
pink glitterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You haven't said which line gives you the error, so I'm going to guess that it's either line 3 (order.line_items.Add) or 12 (Task ord = wc.Order.Add(order);

Both of these lines look like they're adding an object to list, so it's possible that the list itself hasn't been initialised:

order.line_items = new List<OrderLineItem>();
order.line_items.Add(new OrderLineItem()
   ...

Open in new window

0
pink glitterAuthor Commented:
@Chris Stanyon
Hello and thanks for answering! Sorry that i didn't mention it... The line in question is this

order.line_items.Add(new OrderLineItem()
        {
            id=44,
            product_id = 44,
            quantity=2
        }
          );

Thanks again!
0
Chinmay PatelChief Technical NinjaCommented:
On which line you get this exception? Is it when you try to add Products?
order.line_items.Add(new OrderLineItem()

Open in new window


Maybe you would want to first initialize line_items?

i.e.

order.line_items = new line_items()

Open in new window

0
Chris StanyonWebDevCommented:
Hey Pink Glitter,

Yeah ... that makes sense. Basically, you're trying to add a new OrderLineItem to the line_items property, and that property is null, so you'll need to follow my code example and init that property to a new list before adding items to it.

order.line_items = new List<OrderLineItem>();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
It depends on what your collection type is but, you could do something like this:
var order = new Order { status = "pending" };
order.currency = "EUR";
order.line_items = new List<OrderLineItem>() { new OrderLineItem { id = 44, product_id = 44, quantity = 2 } };
order.customer_id = 1;

Open in new window

-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Addins

From novice to tech pro — start learning today.