简体   繁体   English

NullReference Exception在Z61AA5CCE7B7B72EC74B1DFA878AA2BDB56Z.Z6450242531912981C3683CAE888A32A66Z上致电postAsync。

[英]NullReferenceException coming while calling PostAsync on xamarin.Forms

When I try to call, it throws me this error: System.NullReferenceException Message=Object reference not set to an instance of an object.当我尝试调用时,它会给我带来此错误:system.nullReferenceException message =对象引用未设置为object的实例。 Is there something wrong with the code?代码有问题吗? Need Help!需要帮忙!

public Command RegisterUser
    {
        get
        {
            return new Command(async () =>
            {
                UserInfo user = new UserInfo();
                //try
                //{
                    user.UserId = 99;
                    user.LoginId = "aaaa";
                    user.FirstName = "aaa";
                    user.SecondName = "ddd";
                    user.ThirdName = "dd";
                    user.DOB = DateTime.Now.Date;
                    user.PhoneNo = "332323";
                    user.Email = "aa.aa.com";
                    user.Password = "aaaa";

                    string jsonData = JsonConvert.SerializeObject(user);
                    string url = "http://192.168.18.22:44368/api/User";
                    HttpClient client1 = new HttpClient();
                    client1.MaxResponseContentBufferSize = int.MaxValue;
                    StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
                    HttpResponseMessage response1 = await client1.PostAsync(url, content);
                    string result = "";
                    if (response1 != null)
                        result = await response1.Content.ReadAsStringAsync();

                //}

                //catch (Exception ex)
                //{
                //    string eee = "";
                //}

            });
        }
    }

You maybe should check content on null as well as response1, seems like here is the problem.您可能应该在null上检查内容以及响应1,这似乎是问题所在。 More info about stack trace can help also有关堆栈跟踪的更多信息也可以帮助

 if (response1 != null) result = await response1.Content?.ReadAsStringAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin.Forms - HttpClient GetAsync 或 PostAsync 有时需要太长时间 - Xamarin.Forms - HttpClient GetAsync or PostAsync sometimes takes too long Xamarin.Forms App在点击Entry时崩溃,返回“NullReferenceException” - Xamarin.Forms App crashes when clicking on Entry, returns “NullReferenceException” Xamarin.Forms 调用<ios> ()。 不导入名称空间的方法?</ios> - Xamarin.Forms calling On<iOS>(). methods without importing the name space? XML 反序列化的代码在控制台应用程序中有效,但在 Xamarin.Forms 中抛出 NullReferenceException - Code for XML deserialization works in Console App but throws NullReferenceException in Xamarin.Forms 在Xamarin.Forms IOS WebViewRenderer中调用JavaScript函数 - Calling JavaScript function in Xamarin.Forms IOS WebViewRenderer 按钮命令绑定未在Xamarin.Forms中调用binded命令 - Button command binding not calling the binded command in Xamarin.Forms 在Xamarin.Forms中订阅事件时,为什么只能在iOS上收到NullReferenceException? - Why am I getting a NullReferenceException when subscribing to event in Xamarin.Forms, but only on iOS? Xamarin.Forms的背景 - Backgrounding with Xamarin.Forms SingamaR在Xamarin.Forms - SingalR in Xamarin.Forms Mermarin.Forms MergedDictionaries - Xamarin.Forms MergedDictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM