0

I am trying to get a value from firebase node and get it in the onKeyEntered() method for further manipulations.

I understood that I need a custom callback from this answer (https://stackoverflow.com/a/47853774/10076702). However, the code still not working as I expected.

    @Override
    public void onKeyEntered(final String key, final GeoLocation location) {

        Toast.makeText(StudentMapsActivity.this, "onKeyEntered", Toast.LENGTH_SHORT).show();

        readTrack(new MyCallBack() {
            @Override
            public void onCallBack(String track1) {
                try {
                    String track = track1;
                    Log.e("onKeyEntered",track);
                } catch (Exception e) {
                    Log.e("onCallBack","No track specified");
                }
            }
        }, key);
    }


    public interface MyCallBack{
        void onCallBack(String track);
    }

    private void readTrack(final MyCallBack myCallBack, String key){
        driversAvailableDatabaseRef.child(key).child(getString(R.string.track)).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String track = dataSnapshot.getValue(String.class);
                myCallBack.onCallBack(track);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.d("readTrack", databaseError.getMessage());
            }
        });
    }

I always get the toast of "onKeyEntered" when a key enters. However, I get this in the logcat

2019-04-26 14:50:35.677 30310-30310/com.example.android.gpstrackerdemo E/onKeyEntered: green
2019-04-26 14:51:07.039 30310-30310/com.example.android.gpstrackerdemo E/onCallBack: No track specified
2019-04-26 14:52:06.345 30310-30310/com.example.android.gpstrackerdemo E/onCallBack: No track specified

When the key exists already in the database, I get the track name, but when a new key enters I get "No track specified"

This is my database (https://ibb.co/Y77HWqt)

I would appreciate any help please.


Edit: This is what I get after logging the error

readTrack(new MyCallBack() {
            @Override
            public void onCallBack(String track) {
                try {
                    String track1 = track;
                    Log.e("onKeyEntered",track1);
                } catch (Exception e) {
                    Log.e("readTrack","error reading track",e);
                }
            }
        }, key);
2019-04-27 11:07:36.646 11454-11454/com.example.android.gpstrackerdemo E/readTrack: error reading track
    java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.e(Log.java:312)
        at com.example.android.gpstrackerdemo.StudentMapsActivity$6.onCallBack(StudentMapsActivity.java:317)
        at com.example.android.gpstrackerdemo.StudentMapsActivity$7.onDataChange(StudentMapsActivity.java:366)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.1.0:183)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
        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:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Noussa
  • 520
  • 1
  • 4
  • 14
  • 1
    You're swallowing an exception here: `catch (Exception e) {`. I recommend logging it, since it may contain hints as to what is going wrong: `Log.e("readTrack", "Error reading track", e)`. – Frank van Puffelen Apr 26 '19 at 14:21
  • @FrankvanPuffelen Thanks for your reply. I have updated my question with the error. Please help me if you can. – Noussa Apr 27 '19 at 07:11
  • @FrankvanPuffelen It seems that I still can't get the track successfully even after using the custom callback because of the async behavior. How to solve this? – Noussa Apr 27 '19 at 07:16
  • It looks like `track1` may be `null` in `onCallBack`, which may mean it never was retrieved from the database. In that case the problem has nothing to do with the custom callback interface, but is more likely in where you're attaching the listener or how you're extracting the value from the snapshot. I recommend setting a breakpoint, stepping through the code, and seeing what precise line doesn't do what it should do. – Frank van Puffelen Apr 27 '19 at 14:35

0 Answers0