0

I'm trying to get data from a SOAP API so that I can translate to REST API, but I'm getting a NullReferenceException when trying to add an array of a named type.

[HttpGet("{id}")]
public async Task<ActionResult<RouteTable>> GetAllRoutes(string siteId)
{
    BTLAPIRouteServiceClient client = new BTLAPIRouteServiceClient();

    CallContext context = new CallContext();           

    BTLAPIRouteServiceGetAllRoutesResponse result = await client.getAllRoutesAsync(context, dateProcess, siteId);

    BTLAPIRouteTable routeResult = result.response;
    RouteTable routeTable = new RouteTable();

    if (routeResult != null)
    {
        routeTable.deliveryPeriod = routeResult.parmDeliveryPeriod;
        routeTable.driverId = routeResult.parmDriverId;
        routeTable.processId = routeResult.parmProcessId;
        routeTable.routeId = routeResult.parmRouteId;
        routeTable.status = routeResult.parmStatus;
        routeTable.vanId = routeResult.parmVanId;
 
        RouteLine[] routeLine = new RouteLine[routeResult.parmRouteLines.Count()];
        for (int lines = 0; lines < routeResult.parmRouteLines.Count(); lines++)
        {
            routeLine[lines].city = routeResult.parmRouteLines[lines].parmCity; //Null reference on routeLine[lines].city. routeResult.parmRouteLines[lines].parmCity is not null.
            routeLine[lines].custAccount = routeResult.parmRouteLines[lines].parmCustAccount;
            routeLine[lines].custName = routeResult.parmRouteLines[lines].parmCustName;
            routeLine[lines].invoiceId = routeResult.parmRouteLines[lines].parmInvoiceId;
            routeLine[lines].deliveryDate = routeResult.parmRouteLines[lines].parmDeliveryDate;
            routeLine[lines].productType = routeResult.parmRouteLines[lines].parmProductType;
            routeLine[lines].routeId = routeResult.parmRouteLines[lines].parmRouteId;
            routeLine[lines].salesId = routeResult.parmRouteLines[lines].parmSalesId;
            routeLine[lines].volume = routeResult.parmRouteLines[lines].parmVolume;
            routeLine[lines].weight = routeResult.parmRouteLines[lines].parmWeight;
        }

        routeTable.routeLines = routeLine;            

        return routeTable;
    }
    else { return NotFound(); }  
}

I understand you have to initialize the array before using it, but isn't that what I'm doing on this line? routeResult.parmRouteLines.Count() has a value of 1.

RouteLine[] routeLine = new RouteLine[routeResult.parmRouteLines.Count()];

I also tried the method below using a foreach loop and Append, but I had the same error.

foreach (var lines in routeResult.parmRouteLines)
{
    RouteLine[] routeLine = new RouteLine[1];

    routeLine.city = lines.parmCity;
    routeLine.custAccount = lines.parmCustAccount;
    routeLine.custName = lines.parmCustName;
    routeLine.invoiceId = lines.parmInvoiceId;
    routeLine.deliveryDate = lines.parmDeliveryDate;
    routeLine.productType = lines.parmProductType;
    routeLine.routeId = lines.parmRouteId;
    routeLine.salesId = lines.parmSalesId;
    routeLine.volume = lines.parmVolume;
    routeLine.weight = lines.parmWeight;
    //routeTable.routeLines[0] = routeLine;
    routeTable.routeLines.Append(routeLine);
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Toxic
  • 85
  • 8
  • 1
    You may have created an array, but all its elements **are null**. You need to create an instance of `RouteLine`, populate that, and then add it the array. – Mark Rotteveel Mar 04 '22 at 12:36

1 Answers1

1

What you have is an array filled with null values. You have to create an object of type RouteLine, set the values, and then put that in the array.

for (int lines = 0; lines < routeResult.parmRouteLines.Count(); lines++)
{
    var r = new RouteLine();
    r.city = routeResult.parmRouteLines[lines].parmCity;
    r.custAccount = routeResult.parmRouteLines[lines].parmCustAccount;
    routeLine[lines] = r;
}
Magnus
  • 45,362
  • 8
  • 80
  • 118