0

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?!

1
  • Callstack missing, MCVE missing. Commented Jan 8, 2018 at 14:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply. The createOrder is defined by the CreateOrderRequest createOrder = new CreateOrderRequest(); Also ProductCode is not on the 'Orders' level.
@REX8 Sorry, I meant OrderAmount of course. I can see that new CreateOrderRequest(), I'm pointing out you should, in that method (it's called a constructor) do something to create that List or whetever you have your OrderLines in. Alternatively, you can do it after creating that createOrder object (createOrder.OrderLines = new List<OrderLine>() or whatever.
THanks but i still cannot get it to work... If you take a look at stackoverflow.com/questions/46071610/… this is a similar issue but the answers do not seem to work for my scenario.
@REX8 Just add the definition of CreateOrderRequest including its constructor to your question. My answer is based on assumptions and without actual information, nobody can see why this goes wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.