0

I am getting the following error when attempting to upload a new user to the firebase realtime database:

NullReferenceException: Object reference not set to an instance of an object
UserManager.UploadUserDatabase (User user, System.String userId) (at Assets/Scripts/UserManager.cs:34)
UploadUsers.OnAppStart () (at Assets/Scripts/UploadUsers.cs:40)

Here is what I am passing in the UploadUsers:

using UnityEngine;

public class UploadUsers : MonoBehaviour
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    private static void OnAppStart()
    {
        Debug.Log("On Load.");

        //User info
        var user3 = new User("Angel", "Doe", "Angeles", "Team3", "1234567", "fake3@gmail.com", "06/15/1994", "12bd3g");

        UserManager.UploadUserDatabase(user3, "12bd3g");
}

Here is the UserManager method where I am attempting to upload a user to the Firebase Realtime Database. The second Debug log shows me that the user is not null:

public static void UploadUserDatabase(User user, string userId)
{
    Debug.Log("PostUserData method");
    Debug.Log("Data being passed: " + $"{user.firstNme} {user.lastNme} {user.userName} {user.team} {user.pswrd} {user.email} {user.team} {user.birthDate}");
    
    string json = JsonConvert.SerializeObject(user, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

    DBreference.Child("users").Child("profile").Child(userId).SetRawJsonValueAsync(json).ContinueWith(task =>
    {
        if (task.IsCompleted)
        {
            Debug.Log("successfully added user data to firebase");
        }
        else
        {
            Debug.Log("not successfull");
        }
    });
}

and here is the user class:

using System;
using Newtonsoft.Json;

/// <summary>
/// The user class, which gets uploaded to the Firebase Database
/// </summary>

[Serializable] // This makes the class able to be serialized into a JSON
public class User
{
    public string firstNme;
    public string lastNme;
    public string userName;
    public string team;
    public string pswrd;
    public string email;
    public string birthDate;
    public string teamID;

    public User(string firstNme, string lastNme, string userName, string team, string pswrd, string email, string birthDate, string teamID)
    {
        this.firstNme = firstNme;
        this.lastNme = lastNme;
        this.userName = userName;
        this.team = team;
        this.pswrd = pswrd;
        this.email = email;
        this.birthDate = birthDate;
        this.teamID = teamID;
    }

    public User(){
    }
}

Not sure what I am doing wrong. Any help is appreciated.

Robert
  • 23
  • 8
  • Which line is line 34, and which line is line 40? – ProgrammingLlama Mar 23 '21 at 05:01
  • @Llama Line 40 is the following 'UserManager.UploadUserDatabase(user3, "12bd3g");' – Robert Mar 23 '21 at 05:02
  • I'm assuming that one of the items in this line returns `null`: `DBreference.Child("users").Child("profile").Child(userId)` – ProgrammingLlama Mar 23 '21 at 05:03
  • @Llama hmmm... Question then: How do I create the JSON table then? I thought I was creating it on that line that you are referencing – Robert Mar 23 '21 at 05:08
  • No idea, I'm afraid. I've never worked with Firebase. – ProgrammingLlama Mar 23 '21 at 05:09
  • Please debug your code with breakpoints and see yourself which object is `null` exactly ... I would start with `DBreference` .. then go call by call ... if the `DBreference` which is my first guess is actually not `null` then apparently either `Child("users")` doesn't exist or one of the other `Child` nodes – derHugo Mar 23 '21 at 07:38

0 Answers0