REX8
REX8

Reputation: 19

C# Soap Client producing Null Pointer Exception

I am writing a simple C# SOAP client to send an API request but I am receiving a NullPointerException when trying to send a value which looks like it is on a different 'level' of the API request.

I have written the following code to create an instance of the API request:

CreateOrderRequest createOrder = new CreateOrderRequest();

When using SOAP UI ,the XML request contains the following extract:

<data:ProductCode>RX888</data:ProductCode>
 <data:Orders>
               <!--Zero or more repetitions:-->
               <data:OrderLine>
                  <!--Optional:-->
                  <data:OrderAmount>157.65</data:OrderAmount>
                  <data:OrderRef>test</data:OrderRef>
               </data:OrderLine>
</data:Orders>

In my code, I can specify a value to use for the product code by writing the following:

createOrder.ProductCode = "RX888";

When I try to specify a value for the OrderAmount I get a nullpointer exception if I do `createOrder.OrderAmount = "5.99";

I get the same if I try to specify a value for the OrderRef too.

Can anybody help me to get this to work please?!

Upvotes: 0

Views: 340

Answers (1)

oerkelens
oerkelens

Reputation: 5161

Based on your xml, it seems there is no createOrder.OrderAmount.

Your xml seems to indicate that you want to set createOrder.OrderLine[n].OrderAmount, where n is the item in your List(?) of OrderLine.

Since you are getting a NRE, the simplest explanation is that you never instantiated your List, which means it's null.

Adding this to the constructor of your CreateOrderRequest should help:

this.OrderLines = new List<OrderLine>();

Of course, adjust this for your actual definition in your CreateOrderRequest

Upvotes: 1

Related Questions