0

Here is my function:

private void getOrderID() {

    for (int i = 0; i < userID.size(); i++) {
        databaseReference.child(opearatorID).child(userID.get(i)).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    keyStatusRelation.put(childSnapshot.getKey(),childSnapshot.getValue(Order.class).getStatus());
                }
                loadData();

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }

}

This fuction gets an error because data is not loaded in firebase

 public static void loadData() {

    for (int i=0;i<userID.size();i++){
        statusRefrenceLiving.child(userID.get(i)).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    Order order = childSnapshot.getValue(Order.class);
                    if(keyStatusRelation.get(childSnapshot.getKey()).equals("0")||keyStatusRelation.get(childSnapshot.getKey()).equals("2")){
                        bookingmList.add(order);
                        bookingrecyclerView.setAdapter(bookingAdapter);

                    }
                    if(keyStatusRelation.get(childSnapshot.getKey()).equals("1")){
                        bookedmList.add(order);
                        bookedrecycleView.setAdapter(bookedAdapter);

                    }
                    if(keyStatusRelation.get(childSnapshot.getKey()).equals("3")){
                        LivingmList.add(order);
                        LivingrecycleView.setAdapter(livingAdapter);

                    }

                }

            }
            @Override
            public void onCancelled (@NonNull DatabaseError databaseError){

            }
        });

    }

}

Here is logcat

Process: com.teepe.teepestaysmasterapp, PID: 6867 java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference at com.teepe.teepestaysmasterapp.MainActivity$4.onDataChange(MainActivity.java:187) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

It's looping and going only to the second line and not entering onDataChange , can any one tell me what is the problem?

alirza
  • 153
  • 2
  • 13
Chirag Gupta
  • 469
  • 1
  • 7
  • 16

1 Answers1

0

Data is loaded from Firebase asynchronously. This means that your main code continues, while the data is being loaded. And then when the data is available, your onDataChange gets called.

This means that you can't step into onDataChange with a debugger, because the data isn't available yet. If you want to see what happens in onDataChange in a debugger, put a breakpoint on the first line in the method, and let the debugger continue. One the data is available, onDataChange will be called, and the breakpoint will hit.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have edited the question because even if data is loaded later then why is the second func. trying to access it – Chirag Gupta Jun 16 '19 at 00:54
  • That's a completely different problem than you first seemed to describe. Which line is `MainActivity.java:187`? Because that is where you are `get(...)` on a non-existent `Map`. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Jun 16 '19 at 00:58