尝试将数据插入命名类型的数组时出现 NullReferenceException

NullReferenceException when trying to insert data Into array of named type

我正在尝试从 SOAP API 获取数据,以便我可以转换为 REST API,但是在尝试添加命名类型的数组时出现 NullReferenceException .

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

我知道您必须在使用数组之前对其进行初始化,但这不是我在这一行中所做的吗? routeResult.parmRouteLines.Count() 的值为 1。

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

我也尝试了下面的方法,使用 foreach 循环和追加,但我有同样的错误。

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

您拥有的是一个包含 null 个值的数组。您必须创建一个 RouteLine 类型的对象,设置值,然后将其放入数组中。

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