Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NullReferenceException when trying to insert data Into array of named type

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading