We help IT Professionals succeed at work.

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

148 Views
Last Modified: 2018-09-26
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...
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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

Author

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!
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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-

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions