本文介绍了无法在Firebase数据库的child()中为参数'pathString'传递null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的火力基地数据结构:

This is my fire base data structure:

我正在使用以下方法从数据库中检索数据

I am retrieving the data from database using the following method

private DatabaseReference mFirebaseDatabase=FirebaseDatabase.getInstance().getReference();;
    private FirebaseDatabase mFirebaseInstance;

mFirebaseDatabase.child("users").child(userId).child(time).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String value = dataSnapshot.getValue(String.class);
                Log.d("Tag", "Value is: " + value);
                System.out.println("Values is :::::::::::::::::::::::::::::::::::: "+value);


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Failed to read value
                Log.w("Tag", "Failed to read value.", databaseError.toException());


            }
        });

我遇到以下错误

 Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()

请帮助

userID部分,在这里我创建一个新条目以将数据存储到表中

userID part, this is where i create a new entry to store data to the table

 private void createUser(String name, String time, String lat, String lon) {
        // In real apps this userId should be fetched
        // by implementing firebase auth
        if (TextUtils.isEmpty(userId)) {
            userId = mFirebaseDatabase.push().getKey();
            System.out.println("Userid is ------------------------------> "+userId);
        }

        User user = new User(name, time,lat,lon);

        mFirebaseDatabase.child(userId).setValue(user);

        addUserChangeListener();
    }

    /**
     * User data change listener
     */
    private void addUserChangeListener() {
        // User data change listener
        mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);

                // Check for null
                if (user == null) {
                    Log.e(TAG, "User data is null!");
                    return;
                }

                Log.e(TAG, "User data is changed!" + user.name + ", " + user.time);
                System.out.println("User data is changed!" + user.name + ", " + user.time);


            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.e(TAG, "Failed to read user", error.toException());
            }
        });
    }

上面的代码用于生成userId,每次调用插入函数时都会动态创建userId

The above code is used for userId generation,userId is dynamically created every time when a insert function is called

推荐答案

您将ValueEventListener分配到的路径有问题.

There is something wrong with the path you are assigning the ValueEventListener to.

从Firebase数据库图片看,唯一可能导致NullPointerException的问题是使用 .child(userId)或使用 .child(time)

Looking from your Firebase Database picture the only problem that could cause that NullPointerException is when you use .child(userId) or when you use .child(time)

如何解决您的问题:

  • 将此 .child(time)替换为 .child("time")

确保以这种方式检索userId:

make sure you are retrieving the userId this way:

private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

...

protected void onCreate(Bundle savedInstanceState) {
...     
String userId = user.getUid();
...
}

这篇关于无法在Firebase数据库的child()中为参数'pathString'传递null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:08