[英]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.