Firebase数据库

时间:2017-08-22 08:52:15

标签: java android firebase firebase-realtime-database

这是我的火力数据结构: enter image description here

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

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部分,这是我创建一个新数据来存储数据到表

的地方
 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生成,每次调用insert函数时都会动态创建userId

2 个答案:

答案 0 :(得分:0)

确保您将值放入userId或child并且它们至少已实例化(至少应该删除nullpointerException)

答案 1 :(得分:0)

将ValueEventListener分配给的路径有问题。

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

如何解决问题:

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

  • 确保以这种方式检索userId:

    private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
    ...
    
    protected void onCreate(Bundle savedInstanceState) {
    ...     
    String userId = user.getUid();
    ...
    }