Object reference not set to an instance of an object.

Hello guys, i'm having an issue with an app that i'm doing most of the app is working perfectly fine, but when i try to launch a few especifi activities, the app stops working and an error is shown, here an image of the error i just caught it on a try-catch

and this is the code that throws the error:

   try
            {
                BtnRegistrar.Click += async delegate
                {


                    InstructorModel Instructor = new InstructorModel();

                    Instructor.Nombre = TxtNombre.Text;
                    Instructor.Apellido = TxtApellido.Text;
                    Instructor.Estado = "Activo";



                    new Thread(new ThreadStart(() =>
                    {

                        if (SpnCursos.SelectedItem.ToString().Equals("Kung Fu"))
                        {
                            Instructor.Id_Curso = 1;
                        }
                        else if (SpnCursos.SelectedItem.ToString().Equals("Tai Chi"))
                        {
                            Instructor.Id_Curso = 2;
                        }
                        else
                        {
                            Instructor.Id_Curso = 3;
                        }


                    })).Start();


                    HttpClient Cliente = new HttpClient();
                    System.Net.ServicePointManager.Expect100Continue = false;
                    Cliente.DefaultRequestHeaders.ExpectContinue = false;
                    string Url = $"http://matriculawebapitest.azurewebsites.net/api/Instructor?Nombre={Instructor.Nombre}&Apellido={Instructor.Apellido}&Estado={Instructor.Estado}&Id_Curso={Instructor.Id_Curso}";
                    Uri Uri = new Uri(Url);
                    Cliente.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage Respuesta;
                    var Json = JsonConvert.SerializeObject(Instructor);
                    var Contenido = new StringContent(Json, Encoding.UTF8, "application/json");
                    Respuesta = await Cliente.PostAsync(Uri, Contenido);



                    if (Respuesta.StatusCode == System.Net.HttpStatusCode.Accepted)
                    {
                        new Thread(new ThreadStart(() =>
                        {

                            RunOnUiThread(() => { Toast.MakeText(this, "Instructor se ha registrado con exito", ToastLength.Long).Show(); });
                            Vaciar();

                        })).Start();
                    }
                    else
                    {
                        new Thread(new ThreadStart(() =>
                        {

                            RunOnUiThread(() => { Toast.MakeText(this, "No se ha podido registrar al Instructor", ToastLength.Long).Show(); });

                        })).Start();

                    }


                };

            }
            catch (Exception Ex)
            {
                new Thread(new ThreadStart(() =>
                {

                    RunOnUiThread(() => { Toast.MakeText(this, Ex.ToString(), ToastLength.Long).Show(); });

                })).Start();

            }


If someone could help me would be great!

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    This issue is caused by not adding additional logic to ensure that objects have valid values before using them, based on your exception, some value is null in MatriculaV4. Based on the Data: {System.Collections.ListDictionaryInternal} This issue is related to the List<> not instantiated, you can add an breakpoint that used List<> in your project, check the value of List<> if it is null.

    Here is a helpful link about this error.
    https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it

Sign In or Register to comment.