-1

I'm trying to store the value of ds.getKey() to the global variable Common.workerType so that whenever I need to get a value from the database, I wouldn't have to repeat these set of codes over and over again, more ideally just to make it like this

final DatabaseReference dbRefChild = dbRef.child(Common.user_workers_table).child(Common.workerType).child(Common.uid);

I know that the error is showing because it's getting a null data, but how come? I believe I got the right path of the nodes. This is the error I'm getting:

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

    final DatabaseReference newRef = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference newRefChild = newRef.child(Common.user_workers_table);

    newRefChild.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds: dataSnapshot.getChildren()) {
                if(ds.child(Common.uid).exists()) {
                    Common.workerType = ds.getKey();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here's how the database looks like:

Common.user_workers_table refers to Workers

Common.workerType refers to Plumbers

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
ayabbear
  • 308
  • 4
  • 20

2 Answers2

1

You cannot declare Common.workerType as o global variable and simply use it outside the onDataChange() method because it will always be null. This is happening because this method has an asynchronous behaviour, which means that is called even before you are getting the data from the database.

Even if this would have been possible, as I see in your code, you are looping through the entire dataSnapshot object using getChildren() method and every value that you get you assign it to the same variable. In this case, you variable will always have the value of last iteration.

A quick solve for the first problem would be, to use the value of Common.workerType only inside the onDataChange() method or if you need it outside, dive into the asynchronous world and see the last part of my anwser from this post. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    I've been quite thinking about that possibility but I need some assurance, thank you @alex for saving me again! lol. Instead of setting it as a global variable, I had to repeat the codes whenever I need, doesn't sound efficient but looks enough for now for the app to work. – ayabbear Mar 28 '18 at 15:20
0

just add check null like

if(Common.user_workers_table!=null) when you get child

SonVi
  • 91
  • 8
  • however this will only check if the data is null, what I need is to retrieve the data – ayabbear Mar 28 '18 at 03:52
  • the error 'Can't pass null for argument 'pathString' in child()' mean your child key == null or you get wrong child key. Please check it – SonVi Mar 28 '18 at 04:18