System.ArgumentNullException:'值不能为空参数名称:source'

时间:2020-01-17 07:47:00

标签: c# exception

我有一个程序可以连接到站点并使用soap api获取订单列表。但是我有一个非常奇怪的问题。当我尝试获取没有订单的一天的订单,然后尝试获取一天的订单列表时,出现此错误。但是奇怪的是,如果在我遇到错误的地方放一个断点,并逐步评估程序,我不会得到任何错误。怎么会这样这是代码。

https://api.n11.com/ws/OrderService.wsdl

using n11.Deneme.Forms.com.n11.api;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace n11.Deneme.Forms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strStartDate = "18/01/2020";
            string strEndDate = "18/01/2020";

            long totalCountValue = 50;
            int currentPageValue = 0;
            int pageCountValue = 1;
            int pageSizeValue = 50;

            Authentication auth = new Authentication();
            auth.appKey = "b891a6b9-cb97-4a7e-9ffb-f7b1e2a593e8";
            auth.appSecret = "pHCjYYadxwTG64Ej";

            OrderSearchPeriod orderSearchPeriod = new OrderSearchPeriod();
            orderSearchPeriod.startDate = strStartDate;
            orderSearchPeriod.endDate = strEndDate;

            OrderDataListRequest orderDataListRequest = new OrderDataListRequest();
            //orderDataListRequest.status = "1";
            orderDataListRequest.period = orderSearchPeriod;
            //orderDataListRequest.orderNumber = "209524598478";

            PagingData pagingData = new PagingData();
            pagingData.currentPage = currentPageValue;
            pagingData.pageCount = pageCountValue;
            pagingData.pageSize = pageSizeValue;
            pagingData.totalCount = totalCountValue;

            DetailedOrderListRequest request = new DetailedOrderListRequest();
            request.auth = auth;
            request.pagingData = pagingData;
            request.searchData = orderDataListRequest;

            OrderServicePortService port = new OrderServicePortService();
            DetailedOrderListResponse response = port.DetailedOrderList(request);
            List<DetailedOrderData> orderList = response.orderList.ToList();

            foreach (var order in orderList)
            {
                MessageBox.Show(order.totalAmount.ToString() + " - " + order.orderNumber + " - " + order.citizenshipId + " - " + order.createDate);

                long orderIdValue = order.id;

                OrderDataRequest orderDataRequest = new OrderDataRequest();
                orderDataRequest.id = orderIdValue;

                OrderDetailRequest orderdetailrequest = new OrderDetailRequest();
                orderdetailrequest.auth = auth;
                orderdetailrequest.orderRequest = orderDataRequest;


                OrderServicePortService port1 = new OrderServicePortService();
                OrderDetailResponse orderDetailResponse = port1.OrderDetail(orderdetailrequest);
                OrderDetailData orderDetail = orderDetailResponse.orderDetail;

                MessageBox.Show(orderDetail.orderNumber);

                List<OrderSearchData> orderItemList = orderDetail.itemList.ToList();

                foreach (var item in orderItemList)
                {

                    MessageBox.Show(item.shipmentInfo.campaignNumber);

                }


            }

        }



    }
}       

2 个答案:

答案 0 :(得分:0)

如果您在网上遇到错误:

List<DetailedOrderData> orderList = response.orderList.ToList();   //I GOT THE ERROR ON THIS LINE 

然后要做的事情就是看response.orderList如何获得价值。特别是,它是否对线程,任务,计时器,外部事件或其他类似的东西进行了某些处理-这可能意味着它会在从DetailedOrderList的初始返回之后的后不久被填充。可以解释为什么在调试和逐步调试时会起作用(添加一些严重的延迟)​​。

您也可以简单地这样做:

var tmp = response.orderList;
if (tmp == null) throw new InvalidOperationException(
    "hey, response.orderList was null! this is not good!");
List<DetailedOrderData> orderList = tmp.ToList();
return orderList;

这将非常清楚地表明这是正在发生的事情。如果您没有得到此异常,但还有其他异常,则:需要更多调试!

答案 1 :(得分:0)

if(response.orderList == null)
            {
                var temp = button1_Click.PerformClick();
                return temp;           
            }
            else
            {
                List<DetailedOrderData> orderList = response.orderList.ToList();

                return orderList;
            }