Skip to content
Advertisement

How to retrieve the data from firestore in maps

Is there any way to retrieve the data from firestore in Maps. I am trying but its returning null here is my code

docRef = db.collection("Buses").document("Bus1");
Button btn=view.findViewById(R.id.submit);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                bus=documentSnapshot.getData();
            }
        });
        if (!bus.get("ID").toString().matches(busId)) {
            DocumentReference reference = db.collection("Buses").document("Bus2");
            reference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    bus=documentSnapshot.getData();
                }
            });
        }
        if (bus!=null){
            Toast.makeText(getContext(), "Data read Successfully", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(getContext(),MapsActivity.class);
            startActivity(intent);

            CONSTANTS.bus=HomeFragment.this.bus;
            getActivity().finishAffinity();
        }
        else{
            Toast.makeText(getContext(), "Data read Failed", Toast.LENGTH_SHORT).show();
        }

    }
});

But I am getting NullPointerException here Here is the screenshot of the database

strong text

I am not getting i why getdata function is returning null Here is the error log

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.integratedsoftsols.fyp.bustracking.user.HomeFragment$1.onClick(HomeFragment.java:53)
            at android.view.View.performClick(View.java:6274)
            at android.view.View$PerformClick.run(View.java:24859)
            at android.os.Handler.handleCallback(Handler.java:789)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6710)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

Advertisement

Answer

You are getting null because the database query is asynchronous and returns immediately, before the query is complete. Your code continues to execute while the server performs the query, then the callback you provide to addOnSuccessListener is invoked some time later, whenever the query is complete.

As such, the line of code where you log the result is being executed before the query is complete and bus has the value you expect. If you add logging at the line immediately after getData, within the callback (not after the callback), you will see what I mean.

Your code will have to account for the asynchronous nature of the database calls. You won’t be able to use bus until after the query completes.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement