unity3d 空引用异常:对象引用未设置为Firebase连接中的对象示例[重复]

e5nszbig  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(203)
    • 此问题在此处已有答案**:

What is a NullReferenceException, and how do I fix it?(27个答案)
昨天关门了。
我有一个错误从基本保存数据从统一到firebase连接我遵循的文件以及youtube教程也但我不知道为什么该特定行有一个空引用错误

public TMP_Text inputField;
    private string userId;
    public DatabaseReference reference;
    private DatabaseReference mDatabaseRef;
    void Start()
    {
        userId = SystemInfo.deviceUniqueIdentifier;
        reference = FirebaseDatabase.DefaultInstance.RootReference;
    }

    public void writeNewUser()
    {
        User user = new User(name);
        user.username=inputField.text;
      ``
        string json = JsonUtility.ToJson(user);
       mDatabaseRef.Child("Users").Child(userId).SetRawJsonValueAsync(json);// null reference here------

    }

我将子值更改为双精度代码中的字符串,但仍然在特定行中得到错误

hmmo2u0o

hmmo2u0o1#

查看您的代码,我看到两个DatabaseReference变量。您将它们混淆了。

public DatabaseReference reference; // <- DatabaseReference 1
private DatabaseReference mDatabaseRef; // <- DatabaseReference 2
void Start ( )
{
    userId = SystemInfo.deviceUniqueIdentifier;
    reference = FirebaseDatabase.DefaultInstance.RootReference; // <- Set DatabaseReference 1
}

public void writeNewUser ( )
{
    User user = new User(name);
    user.username = inputField.text;
    string json = JsonUtility.ToJson(user);
    mDatabaseRef.Child ( "Users" ).Child ( userId ).SetRawJsonValueAsync ( json ); // <- Using DatabaseReference 2
}

请改为执行此操作。

public DatabaseReference reference; // <- DatabaseReference 1
void Start ( )
{
    userId = SystemInfo.deviceUniqueIdentifier;
    reference = FirebaseDatabase.DefaultInstance.RootReference; // <- Set DatabaseReference 1
}

public void writeNewUser ( )
{
    User user = new User(name);
    user.username = inputField.text;
    string json = JsonUtility.ToJson(user);
    reference .Child ( "Users" ).Child ( userId ).SetRawJsonValueAsync ( json ); // <- Using DatabaseReference 1
}

相关问题