0

When I try to call , it throws me this error: System.NullReferenceException Message=Object reference not set to an instance of an 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 = "";
                //}

            });
        }
    }
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
Candy
  • 407
  • 1
  • 7
  • 19
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Klaus Gütter Aug 20 '22 at 07:20
  • @KlausGütter no it did'nt. – Candy Aug 20 '22 at 11:22
  • So you already debugged your code? On which line did the exception occur? And you inspected the variables? – Klaus Gütter Aug 20 '22 at 11:28
  • yes i have debugged it. the error occuring on line where i calling PostAsync method. – Candy Aug 20 '22 at 15:14
  • 1
    Is that line actually throwing an exception? Or are you getting that message retuned from the server? There is nothing on that line that appears to be null. If it’s an exception what does the stack trace show? – Jason Aug 20 '22 at 15:16
  • @Jason following error message coming when i added try catch block in this code. – Candy Aug 21 '22 at 09:04
  • Failed to connect to /192.168.18.22:44368 – Candy Aug 21 '22 at 09:04
  • Then you obviously have a connectivity issue – Jason Aug 21 '22 at 12:07

1 Answers1

0

You maybe should check content on null as well as response1, seems like here is the problem. More info about stack trace can help also

 if (response1 != null) result = await response1.Content?.ReadAsStringAsync();
IKomarovLeonid
  • 352
  • 1
  • 9