locked
System.NullReferenceException: Object reference not set to an instance of an object. RRS feed

  • Question

  • User337094 posted

    ` public void OnDataChange(DataSnapshot snapshot) { if (snapshot.Value != null) { var child = snapshot.Children.ToEnumerable(); ModelPosts.Clear(); foreach (DataSnapshot UserModels in child) { UsePostModel usePostModel = new UsePostModel { PostId = UserModels.Key, PostCaption = UserModels.Child("PostCaption").Value.ToString(), DateCreated = UserModels.Child("DateCreated").Value.ToString(), PostImage = UserModels.Child("PostImage").Value.ToString(),

                        UserId = UserModels.Key,
                        FirstName = UserModels.Child("FirstName").Value.ToString(),
                        LastName = UserModels.Child("LastName").Value.ToString(),
                        UserName = UserModels.Child("UserName").Value.ToString(),
                        UserEmail = UserModels.Child("UserEmail").Value.ToString(),
                        UserContact = UserModels.Child("UserContact").Value.ToString(),
                        ProfileImages = UserModels.Child("ProfileImages").Value.ToString(),
                        Height = UserModels.Child("Height").Value.ToString(),
                        Weight = UserModels.Child("Weight").Value.ToString(),
                        Feet_Size = UserModels.Child("Feet_Size").Value.ToString(),
                        Complexion = UserModels.Child("Complexion").Value.ToString(),
                        Biography = UserModels.Child("Biography").Value.ToString(),
                        Age = UserModels.Child("Age").Value.ToString(),
                        UserCountry = UserModels.Child("UserCountry").Value.ToString()
    
    
                };
                ModelPosts.Add(usePostModel);
                }
                ModelsPostRetrieve.Invoke(this, new ModelsPostEvent { usePostModels = ModelPosts });
            }`
    
    Saturday, April 4, 2020 6:01 AM

Answers

  • User366895 posted

    There are Visual Studio extensions like Developer Analytics Tools that will give you a warning if you don't check for a reference where can be null, very helpful

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 9, 2020 3:49 PM

All replies

  • User382871 posted

    System.NullReferenceException: Object reference not set to an instance of an object. It means the code used an object reference variable that was set to null. Please try to add breakpoints for debugging to get the null object.

    Monday, April 6, 2020 8:23 AM
  • User337094 posted

    Yes, I found the error, it was from the way I was querying items from my database

    Monday, April 6, 2020 8:25 AM
  • User382871 posted

    it was from the way I was querying items from my database Do you mean the 'child' of this command or the 'snapshot'? Please check the parameter when calling the OnDataChange method. var child = snapshot.Children.ToEnumerable();

    Tuesday, April 7, 2020 7:19 AM
  • User337094 posted

    @Jarvan no I meant the way my json data was structured was causing the error

    Tuesday, April 7, 2020 7:22 AM
  • User382871 posted

    Please make sure the references are not null when adding data.

    Here is a similar issue case, you can refer to: https://stackoverflow.com/questions/22294422/object-reference-not-set-to-an-instance-of-an-object-error-in-json

    Wednesday, April 8, 2020 7:52 AM
  • User366895 posted

    There are Visual Studio extensions like Developer Analytics Tools that will give you a warning if you don't check for a reference where can be null, very helpful

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 9, 2020 3:49 PM