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.