如何避免结果为空时我的应用程序崩溃?

时间:2019-07-16 08:32:05

标签: c#

我的应用程序总是崩溃,因为我收到以下错误消息:

  

System.NullReferenceException(对象引用未设置为实例   一个对象。)

我在代码中添加了if (result != null),但是我的应用程序仍然崩溃。

如果结果为空,该怎么办?如何避免我的应用程序崩溃?

    private async Task ValidateAndroidReceiptAsync(string purchaseditemcurrencycode, int purchaseditempurchaseprice, string signeddata, string signature)
    {
        var result = await PlayFabClientAPI.ValidateGooglePlayPurchaseAsync(new ValidateGooglePlayPurchaseRequest()
        {
            CurrencyCode = purchaseditemcurrencycode,
            PurchasePrice = (uint)purchaseditempurchaseprice,
            ReceiptJson = signeddata,
            Signature = signature
        });


        if (result != null)
        {
          if (result.Error != null)
          {    
            ShowMessage = result.Error.Error.ToString();
          }
          else
          {
            ShowMessage = "PlayFab successful";
          }
        }
        else
        {
          ShowMessage = "result = null";
        }
    }

0 个答案:

没有答案