0

This is my fire base data structure: enter image description here

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());


            }
        });

i am getting the below error

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

please help

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());
            }
        });
    }

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

2 Answers2

0

make sure you're placing a value into either userId or child and that they're at least instantiated (should at least get rid of the nullpointerException)

Alex Gold
  • 105
  • 8
0

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

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)

How to fix your problem:

  • replace this .child(time) to .child("time")

  • make sure you are retrieving the userId this way:

    private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
    ...
    
    protected void onCreate(Bundle savedInstanceState) {
    ...     
    String userId = user.getUid();
    ...
    }
    
Catalin Ghita
  • 794
  • 8
  • 26