对象引用未设置为对象的实例 - webservices

时间:2012-04-10 21:18:54

标签: c# web-services object instance webservice-client

我的Web服务中出现此错误,即使我看过有关null问题的文章,我也无法找到解决此错误的方法

这是我的代码:

    SendInvUpdate.InvServices.UpdateRatePackagesRequest urq = new SendInvUpdate.InvServices.UpdateRatePackagesRequest();
    SendInvUpdate.InvServices.UpdateRatePackagesOperationResponse ors = new SendInvUpdate.InvServices.UpdateRatePackagesOperationResponse();


    protected void Page_Load(object sender, EventArgs e)
    {
        Int64 HID = 819942;
        Int64 HRID = 154482;

        SendInvUpdate.InvServices.UpdateRatePackagesRequest request = new SendInvUpdate.InvServices.UpdateRatePackagesRequest();

        ChannelManagerAccount account = new ChannelManagerAccount();
        request.HotelId = HID;

        int avail = 4;
        DateTime frodte = Convert.ToDateTime("2012-04-12");
        DateTime todte = Convert.ToDateTime("2012-04-30");
        int NoofRatePackages = 3;
        UpdateRatePackageRequest[] RatePackages = new UpdateRatePackageRequest[NoofRatePackages];
        string res;
        request.RatePackages = new UpdateRatePackageRequest[NoofRatePackages];
        request.RatePackages = RatePackages;

        UpdateRatePackageRequest rp = new UpdateRatePackageRequest();

        for (int i = 0; i < NoofRatePackages; i++)
        {        
            rp.RatePackageId = HRID;        

            RateDetails[] Rates = new RateDetails[NoofRatePackages];

            rp.Rates = new RateDetails[NoofRatePackages];
            rp.Rates = Rates;

            RateDetails rd = new RateDetails();

            for (int j = 0; j < NoofRatePackages; j++)
            {
                rd.Availability = avail;

                rd.AvailabilityApplicationType = SendInvUpdate.InvServices.AvailabilityApplicationType.SET;

                rd.FromDate = frodte;

                rd.ToDate = todte;

            }

        }

       SendInvUpdate.InvServices.InventoryServiceClient icc = new SendInvUpdate.InvServices.InventoryServiceClient();            
        // ( *Line where the error appears*)
        ors = icc.UpdateRatePackages(request); 

        res = ors.Results.ToString();          
    }

我知道它与RateDetails数组有关,并将RateDetails的值初始化为该请求的瞬间,但我不知道如何或如何处理。在调试模式下,当查看rp的值时,rp.Rates [0]没有值,rp.Rates [1],rp.Rates [2]也是RatePackages [0],RatePackages [1],RatePackages [2]是也是null所以我有一种强烈的感觉,这就是我的问题,但我不知道如何修复。

对于我出错的地方有任何见解会很感激!

1 个答案:

答案 0 :(得分:2)

将pagecload代码中的icc服务引用初始化移动为

SendInvUpdate.InvServices.InventoryServiceClient icc = new SendInvUpdate.InvServices.InventoryServiceClient(); 
ors = icc.UpdateRatePackages(request); ( Line where the error appears)

我不是asp.net的专家,但如果不将它们保存在某个持久容器中(搜索SESSION,VIEWSTATE),则无法将代码基于全局变量

编辑: 这里也出现了一些错误

   UpdateRatePackageRequest[] RatePackages = new UpdateRatePackageRequest[NoofRatePackages]; 
   string res; 
   request.RatePackages = new UpdateRatePackageRequest[NoofRatePackages]; 
   request.RatePackages = RatePackages; 

应该只是request.RatePackages = new UpdateRatePackageRequest[NoofRatePackages];

EDIT2: 您向InventoryServiceClient发送一个包含UpdateRatePackageRequest数组的对象request,但在创建数组后,您不会设置任何UpdateRatePackageRequest实例。所以我认为在读取数组中的值时,InventoryServiceClient失败了。 我将尝试以这种方式改变你的for循环

   for (int i = 0; i < NoofRatePackages; i++)       
   {               
        UpdateRatePackageRequest rp = new UpdateRatePackageRequest(); 
        request.RatePackages[i] = rp;
        ....

与RateDetails相同的错误。您创建数组,但不要使用RateDetails的实际实例

设置数组的任何值
        for (int j = 0; j < NoofRatePackages; j++) 
        { 
            RateDetails rd = new RateDetails();  
            rp.Rates[j] = rd;
            ....